Enter help to see the basic Operations Command:
One: operation of the DB
1. Creating a MongoDB database column such as creating a database named Pockbox use Pockbox, MongoDB does not have a command to create a database
2, switch/CREATE database use YOURDB; The current database is created automatically when a collection (table) is created
3, query all the database show DBS;
4, delete the current use database Db.dropdatabase ();
5, cloning the database from the designated host db.clonedatabase ("127.0.0.1"); Clones data from a database on a specified machine to the current database
6. Copy the specified database data from the specified machine to a database Db.copydatabase ("MyDB", "temp", "127.0.0.1"), and copy the native mydb data to the TEMP database
7, repair the current database db.repairdatabase ();
8. View the currently used database Db.getname (); db The DB and GetName methods are the same effect and can query the currently used database
9, display the current DB status Db.stats ();
10, the current DB version db.version ();
11, view the current DB link machine address Db.getmongo ();
Two: The operation of the collection
Collection Aggregation Collection
1. Create a Clustered collection (table) db.createcollection ("Collname", {size:20, Capped:5, max:100});
2. A clustered Set (table) Db.getcollection ("account") with the specified name;
3, get all the current DB aggregation set Db.getcollectionnames ();
4, display the current DB all Clustered index status db.printcollectionstats ();
Show Collections: Displays a collection in the current database (similar to a table in a relational database show tables)
Show Users: Show user
Db.help (): Show database Operations Command with a lot of commands
Db.foo.help (): Displays the set Operation command, as well as a lot of commands, Foo refers to the current database, a collection called Foo, not the real meaning of the command
Db.foo.find (): Data lookup for the Foo collection in the current database (all data is listed because there are no conditions)
Db.foo.find ({a:1}): Looks for the Foo collection in the current database, provided the data has a property called a, and A has a value of 1
Three: User-related actions
1. Add a user db.adduser ("name");d b.adduser ("UserName", "pwd123", true); Add user, set password, read-only
2, database authentication, security mode Db.auth ("UserName", "123123");
3, show all current users show user;
4, delete user Db.removeuser ("UserName");
1, the error message before the query Db.getpreverror ();
2, clear Error Record db.reseterror ();
Four: Modify, add, delete collection data
1. Add Db.users.save ({name: ' Zhangsan ', age:25, sex:true}), data column of added data, not fixed, according to the data added
2. Modify Db.users.update ({age:25}, {$set: {name: ' ChangeName '}}, False, True); equivalent: Update users set name = ' ChangeName ' where a GE = 25;
Db.users.update ({name: ' Lisi '}, {$inc: {age:50}}, False, True); equivalent: Update users set age = Age + where name = ' Lisi ';
Db.users.update ({name: ' Lisi '}, {$inc: {age:50}, $set: {name: ' HoHo '}}, False, True); equivalent: Update users set age = Age + 50, Name
= ' HoHo ' WHERE name = ' Lisi ';
3, delete db.users.remove ({age:132});
4, query Modify Delete db.users.findAndModify ({
Query: {age: {$gte: 25}},
Sort: {Age:-1},
Update: {$set: {name: ' A2 '}, $inc: {age:2}},
Remove:true
});
Db.runcommand ({findandmodify: "Users",
Query: {age: {$gte: 25}},
Sort: {Age:-1},
Update: {$set: {name: ' A2 '}, $inc: {age:2}},
Remove:true
});
Update or remove one of them is a required parameter; Additional parameters are optional.
Parameter detail Default value query
Query Filter Criteria
{}
Sort
If multiple documents conform to a query filter, the first-ranked object will be selected in the arrangement specified by the parameter, and the object will be manipulated
{}
Remove
If true, the selected object will be deleted before it is returned
N/A
Update
A Modifier object
N/A
New
If true, the modified object is returned instead of the original object. In the delete operation, the parameter is ignored.
False
Fields
See Retrieving a subset of fields (1.5.0+)
All fields
Upsert
Creates a new object if the query result is empty. Example (1.5.4+)
False
Statement block operations
1. Simple Hello World
Print ("Hello world!");
This notation calls the print function, and writes "Hello world!" directly The effect is the same;
2. Convert an object to JSON
Tojson (New Object ());
Tojson (New Object (' a '));
3. Loop Add data
> for (var i = 0; i < i++) {
... db.users.save ({name: "u_" + I, age:22 + i, sex:i% 2});
... };
This adds 30 data to the loop, and you can also omit the notation
> for (var i = 0; i < i++) Db.users.save ({name: "u_" + I, age:22 + i, sex:i% 2});
is also possible, when you use the Db.users.find () query, display multiple data and can not be displayed on a page, the use of it to view the next page of information;
4. Find Cursor Query
>var cursor = Db.users.find ();
> while (Cursor.hasnext ()) {
Printjson (Cursor.next ());
}
This allows you to query all the users information, as well as to write
var cursor = Db.users.find ();
while (Cursor.hasnext ()) {Printjson (cursor.next);}
You can also omit the {} number
5. Foreach Iteration Loops
Db.users.find (). ForEach (Printjson);
A function must be passed in foreach to process data information for each iteration
6. Treat the Find cursor as an array
var cursor = Db.users.find ();
CURSOR[4];
Get the data for subscript index 4
Since it can be treated as an array, its length can be obtained: Cursor.length (); or cursor.count ();
So we can also display the data in a loop
for (var i = 0, Len = c.length (); i < Len; i++) Printjson (C[i]);
7. Convert the Find cursor to an array
> var arr = db.users.find (). ToArray ();
> Printjson (arr[2]);
To convert it to an array using the ToArray method
8. Customize our own query results
Only age <= 28 is displayed and only the Age column data Db.users.find ({age: {$lte:}}, {age:1}) is displayed. ForEach (Printjson);
Db.users.find ({age: {$lte: +}}, {age:true}). ForEach (Printjson);
Excludes Age's Column db.users.find ({age: {$lte: +}}, {Age:false}). ForEach (Printjson);
9. The Foreach transfer function displays information Db.things.find ({x:4}). foreach (function (x) {print (Tojson (x));});
Five: Clustered collection query
1, query all Records db.userInfo.find (); equivalent: select* from UserInfo; The default is 20 records per page, and when not shown, you can query the next page of data with it iteration commands. Attention:
Type it command cannot take ";" but you can set the size of the data displayed per page, with dbquery.shellbatchsize= 50, so that each page shows 50 records.
2. The duplicate data db.userInfo.distinct ("name") of a column in the current collection after the query is removed; the same data in name is filtered out to the same amount: Select Distict name from UserInfo;
3. Query age = 22 Record Db.userInfo.find ({"Age": 22}), equivalent to: SELECT * from userInfo where age = 22;
4. Query age > 22 record Db.userInfo.find ({age: {$gt: 22}}), equivalent to: SELECT * from UserInfo where age >22;
5. Query age < 22 record Db.userInfo.find ({age: {$lt: 22}}), equivalent to: SELECT * from UserInfo where age <22;
6. Query Db.userInfo.find ({age: {$gte: 25}) for the record of age >= 25, equivalent to: SELECT * from UserInfo where the age >= 25;
7, Query the age <= 25 record Db.userInfo.find ({age: {$lte: 25}});
8. Query age >= 23 and age <= 26db.userinfo.find ({age: {$gte: $, $lte: 26}});
9, query name contains MONGO data Db.userInfo.find ({name:/mongo/}),//equivalent to a percent of select * from UserInfo where the name like '%mongo% ';
10. Query name Db.userInfo.find ({name:/^mongo/}) Starting with MONGO, select * from UserInfo where name is like ' mongo% ';
11. Query the specified column name, age data Db.userInfo.find ({}, {name:1, age:1}), equivalent to: select Name, age from UserInfo, and of course name can also use True or FALSE,
When using Ture, the river name:1 the same effect, if False is to exclude name, display column information other than name.
12. Query the specified column name, age data, age > Db.userInfo.find ({age: {$gt: +}}, {name:1, age:1}), equivalent to: select Name, age from UserInfo
where Age >25;
13. Sort Ascending by Age: Db.userInfo.find (). Sort ({age:1}); Descending: Db.userInfo.find (). Sort ({Age:-1});
14. Query name = Zhangsan, age = 22 Data db.userInfo.find ({name: ' Zhangsan ', age:22}); equivalent: SELECT * from userInfo WHERE name =
' Zhangsan ' and age = ' 22 ';
15, query the first 5 data db.userInfo.find (). Limit (5); equivalent: selecttop 5 * from UserInfo;
16, the query 10 after the data Db.userInfo.find (). Skip (10); equivalent: SELECT * from UserInfo where ID not in (selecttop * from UserInfo);
17, query the data between 5-10 db.userInfo.find (). Limit (5); can be used for pagination, limit is Pagesize,skip is the first few pages *pagesize
18, or with the query Db.userInfo.find ({$or: [{age:22}, {age:25}]}), equivalent to: SELECT * from UserInfo where is age = n or age = 25;
19, query the first Data Db.userInfo.findOne (); equivalent to: Selecttop 1 * from Userinfo;db.userinfo.find (). limit (1);
20. Query the number of record bars for a result set Db.userInfo.find ({age: {$gte: +}}). count (); equals: SELECT count (*) from UserInfo where age >= 20;
21. Sort by a column db.userInfo.find ({sex: {$exists: true}}). count (); equivalent to: select COUNT (Sex) from userInfo;
VI: Index
1. Create index Db.userInfo.ensureIndex ({name:1});d B.userinfo.ensureindex ({name:1, TS:-1});
2, query the current aggregate collection all Indexes db.userInfo.getIndexes ();
3, view the total index record size db.userInfo.totalIndexSize ();
4, read all index information of the current collection Db.users.reIndex ();
5, delete the specified index Db.users.dropIndex ("Name_1");
6, delete all Index index db.users.dropIndexes ();
Seven View Help information
1. Help view command Prompt help
Db.help ();
Db.yourColl.help ();
Db.youColl.find (). Help ();
Rs.help ();
View Aggregate Collection basic information
1, check the help Db.yourColl.help ();
2, query the current collection of data bar number Db.yourColl.count ();
3, view data space size db.userInfo.dataSize ();
4. Get the DB Db.userInfo.getDB () where the current aggregation set resides;
5, get the current gathering state db.userInfo.stats ();
6, get the aggregate aggregate size db.userInfo.totalSize ();
7, aggregate storage space size db.userInfo.storageSize ();
8. Shard Version Information Db.userInfo.getShardVersion ()
9, Aggregation set rename Db.userInfo.renameCollection ("Users"); Rename UserInfo to users
10. Delete the current aggregation set Db.userInfo.drop ();
Main operations of MongoDB