Basic Operations Command:
Show DBS: Display Database list
Show Collections: Displays a collection in the current database (similar to a table in a relational database)
Show Users: Show user
Use <db name>: Switch the current database, which is the same as the meaning in Ms-sql
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
MongoDB does not have a command to create a database, but there are similar commands.
For example, if you want to create a "myTest" database, run the use myTest command first, then do something (such as: db.createcollection (' user ')) so that you can create a database called "MyTest".
Database Common Commands
1. Help View command Prompt
Help
Db.help ();
Db.yourColl.help ();
Db.youColl.find (). Help ();
Rs.help ();
2. Switch/CREATE Database
Use Yourdb; The current database is created automatically when a collection (table) is created
3. Query all databases
Show DBS;
4. Delete the currently used database
Db.dropdatabase ();
5. Cloning a database from a specified 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"), copy the native mydb data to the TEMP database
7. Repairing the current database
Db.repairdatabase ();
8. View the database currently in use
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. Current DB version
Db.version ();
11. View the current DB link machine address
Db.getmongo ();
Collection Aggregation Collection
1. Create a Clustered collection (table)
Db.createcollection ("Collname", {size:20, Capped:5, max:100});
2. Get a clustered collection with the specified name (table)
Db.getcollection ("account");
3. Get all the aggregation sets of the current DB
Db.getcollectionnames ();
4. Displays the status of all clustered indexes in the current DB
Db.printcollectionstats ();
User-related commands
1. Add a user
Db.adduser ("name");
Db.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 Users;
4. Delete users
Db.removeuser ("UserName");
Other
1, the error message before the query
Db.getpreverror ();
2. Clear the Error Record
Db.reseterror ();
View Aggregate Collection basic information
1. View Help
Db.yourColl.help ();
2. Query the number of data bars in the current collection
Db.yourColl.count ();
3. View data space size
Db.userInfo.dataSize ();
4. Get the db where the current aggregation collection resides
Db.userInfo.getDB ();
5. Get the current aggregation status
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. Rename the Aggregation collection (rename userinfo to users)
Db.userInfo.renameCollection ("users");
10. Delete the current aggregation collection
Db.userInfo.drop ();
Clustered Collection Query
1. Check all records
Db.userInfo.find (); Equivalent: select* from UserInfo;
The default is 20 records per page, and you can query the next page of data with the IT iteration command when the display is not displayed. Note: Type the 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. Duplicate data for a column in the current clustered collection after the query is removed
Db.userInfo.distinct ("name"), filtering out the same data in name is equivalent to: Select Distict name from UserInfo;
3. Check the record of age = 22
Db.userInfo.find ({"Age": 22}), equivalent to: SELECT * from userInfo where age = 22;
4. Check the records of age > 22
Db.userInfo.find ({age: {$gt: 22}}), equivalent to: SELECT * from UserInfo where >22;
5. Check the records of age < 22
Db.userInfo.find ({age: {$lt: 22}}), equivalent to: SELECT * from UserInfo where <22;
6. Check the records of age >= 25
Db.userInfo.find ({age: {$gte: 25}}), equivalent to: SELECT * from UserInfo where age >= 25;
7. Check the records of age <= 25
Db.userInfo.find ({age: {$lte: 25}});
8. Query age >= 23 and age <= 26
Db.userInfo.find ({age: {$gte: $lte: 26}});
9. Query the data containing MONGO in name
Db.userInfo.find ({name:/mongo/});//equivalent to%%select * from UserInfo where name like '%mongo% ';
10, query the name in the beginning of MONGO
Db.userInfo.find ({name:/^mongo/}); select * from userInfo where name 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 be true or false, as the river Ture effect with Name:1, If you use False to exclude name, display column information other than name.
12. Query the specified column name, age data, age > 25
Db.userInfo.find ({age: {$gt: +}}, {name:1, age:1}); equivalent: select Name, age from UserInfo where >25;
13. Sort by age
Ascending: 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 to: 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, query 10 after the data
Db.userInfo.find (). Skip (10), equivalent to: SELECT * from UserInfo where ID not in (selecttop to UserInfo);
17, query the data between 5-10
Db.userInfo.find (). Limit (5); can be used for paging, limit is Pagesize,skip is the first page *pagesize
18, OR and query
Db.userInfo.find ({$or: [{age:22}, {age:25}]}), equivalent to: SELECT * from userInfo where age = n or age = 25;
19. Query the first piece of 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: select count (Sex) from userInfo;
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 ();
Modify, add, and delete collection data
1. Add
Db.users.save ({name: ' Zhangsan ', age:25, sex:true}), data column of the added data, not fixed, according to the data added
2. Modification
Db.users.update ({age:25}, {$set: {name: ' ChangeName '}}, False, True); equivalent: Update users set name = ' ChangeName ' where age = 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 modification Delete
Db.users.findAndModify ({query: {age: {$gte: +}}, Sort: {Age:-1}, Update: {$set: {name: ' A2 '}, $inc: {age:2}}, remove: true});
Db.runcommand ({findandmodify: "Users", query: {age: {$gte: +}}, Sort: {Age:-1}, Update: {$set: {name: ' A2 '}, $inc: {AG E:2}}, remove:true});
Update or remove one of them is a required parameter; Additional parameters are optional.
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 Jsontojson (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 can also omit the notation for parentheses > for (var i = 0; i < i++) Db.users.save ({name: "u_" + I, age:22 + i, sex:i% 2}); Yes, you can use it to view the next page of information when you query with Db.users.find () and display multiple data without a single page display;
4. Find cursor query >var cursor = Db.users.find ();> while (Cursor.hasnext ()) {Printjson (Cursor.next ());} In this way, all the users information is queried, as can the var cursor = Db.users.find (), while (Cursor.hasnext ()) {Printjson (cursor.next);} You can also omit the {} number
5. Foreach Iteration loop Db.users.find (). foreach (Printjson); a function must be passed in foreach to process data information for each iteration
6. The find cursor is treated as an array of var cursor = Db.users.find (); cursor[4]; the data with subscript index 4 can be processed as an array, so it can be obtained in length: Cursor.length (); or Cursor.count (); So we can also display the data for (var i = 0, Len = c.length (); i < Len; i++) Printjson (c) in the loop;
7. Convert the Find cursor to an array of > var arr = db.users.find (). ToArray ();> Printjson (arr[2]); convert it to an array using the ToArray method
8. Customize our own query results only show age <= 28 and only the age of this column of data Db.users.find ({age: {$lte: +}}, {age:1}). ForEach (Printjson);d B.users.find ({age: {$lte: Age:true}). foreach (Printjson); Excludes the Age 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));});
MongoDB Basic Operations Summary