MongoDB Common Commands

Source: Internet
Author: User

After the successful start of MongoDB, and then open a command line window input MONGO, you can do some of the database operations.

1. Enter Help to see the basic Operation 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;: Toggles the current database, which and ms-  1}): Find 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".


2, the database commonly used 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 created3, query all databases show DBS;4, delete the currently used database Db.dropdatabase ();5, clone the database from the specified host Db.clonedatabase ("127.0.0.1"); Clones data from a database on a specified machine to the current database6, copying the specified database data from the specified machine to a database Db.copydatabase ("MyDB","Temp","127.0.0.1"); Copy the MyDB data from this machine to the TEMP database7, repair the current database db.repairdatabase ();8, view the database currently in use Db.getname ();d B; The DB and GetName methods are the same effect and can query the currently used database9, display the current DB status Db.stats ();10, current DB version db.version ();11, view the current DB link machine address Db.getmongo ();

3. Collection Aggregation collection:

1, 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 ();

4. User-Related:

1, add a user db.adduser ("name");d B.adduser ("UserName","pwd123",true);#add user, set password, read-only2, database authentication, security mode Db.auth ("UserName","123123");3, show all current user show users;4, delete user Db.removeuser ("UserName");

5. Other:

1, the error message before the query Db.getpreverror (); 2, clear Error Record db.reseterror ();

6. 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, Clustered Collection rename db.userInfo.renameCollection (" Users " ); Rename UserInfo to users10, delete the current clustered collection Db.userInfo.drop ();

7. Aggregate Collection query:

1, query all Records db.userInfo.find (); equivalent: Select*from UserInfo; The default is 20 records per page, and you can use it iteration commands to query the next page of data 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, using Dbquery.shellbatchsize= 50This will show 50 records per page. 2, the duplicate data db.userInfo.distinct for a column in the current clustered collection after the query is removed ("name"filter out the same data in name equivalent to: Select Distict name from UserInfo;3. Query age =22 of Records Db.userInfo.find ({" Age": 22}); equivalent to: Select* from userInfo where age = 22;4. Query Age >22 of the record Db.userInfo.find ({age: {$GT: 22}}); equivalent to: Select* FROM UserInfo where age >22;5, Query Age < 22 for records Db.userInfo.find ({age: {$lt: 22}}); equivalent to: Select* FROM UserInfo where age <22;6. Query Age >=25 of the record Db.userInfo.find ({age: {$gte: 25}}); equivalent to: Select* FROM UserInfo where age >= 25;7. Query Age <=25 of the record Db.userInfo.find ({age: {$lte: 25}});8. Query age >= 23 and age <= 26Db.userInfo.find ({age: {$gte: 23,$lte: 26}});9, the query name contains the MONGO data Db.userInfo.find ({name:/mongo/}); equivalent to%%:select * from UserInfo where name like '%mongo%';10, Query the name Db.userInfo.find ({name: MONGO) that begins with/^mongo/}); equivalent to: 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 used with true or false, as in the case of Ture River Name:1 effect, if False is to exclude name, display column information other than name. 12. Query the specified column name, age data, age > 25Db.userInfo.find ({age: {$GT: +-}}, {name:1, age:1}); equivalent to: select Name, age from UserInfo where age>25;13, sorted by age Ascending: Db.userInfo.find (). Sort ({ages: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: Selecttop5 *From UserInfo;16, query the data after 10 Db.userInfo.find (). Skip (10); equivalent to: Select* FROM UserInfo where ID notinch(Selecttop10 *from userInfo);17, the query in 5-data between 10 db.userInfo.find (). Limit (Ten). Skip (5); can be used for pagination, limit is Pagesize,skip is the first page*pageSize18, OR and query Db.userInfo.find ({$or: [{age:22}, {age:25}]}); equivalent to: Select* from userInfo where age = 25 or;19, query the first Data Db.userInfo.findOne (); equivalent to: Selecttop1 *From userinfo;db.userinfo.find (). Limit (1);20, query the number of record bars for a result set Db.userInfo.find ({age: {$gte: 25}}). count (); equivalent to: SELECT COUNT (*) from UserInfo where age >= 20if you want to return the number of records after the limit, use count (true) or count (not 0) db.users.find (). Skip (). Limit (5). Count (true);21st, sort by a column db.userInfo.find ({sex: {$exists:true}}). count (); equivalent: select count (Sex) from UserInfo;

8. Index:

111, 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 ();

9, modify, add, delete collection data:

1, add Db.users.save ({name: ' Zhangsan ', Age:Sex:true}); The data column of the added data is not fixed, depending on the data added2, modify the query criteria for db.collection.update (criteria, objnew, Upsert, Multi) criteria:update, similar to the objnew in the Where in SQL update query: The update object and some updated operators, such as$,$inc... ) and so on, can also be understood as SQL update query within the set after. Upsert: If there is no record of update, insert objnew,true as INSERT, default is False, do not insert. Multi:mongodb default is False, only update the first record found, if this parameter is true, the condition is checked out all the records are updated. Db.users.update ({age:25}, {$set: {name: ' ChangeName '}},false,true); equivalent: Update users set name= ' ChangeName ' where age = 25;d b.users.update ({name: ' Lisi '}, {$inc: {age:50}},false,true); equivalent: Update users set age= age + WHERE name =' Lisi ';d b.users.update ({name: ' Lisi '}, {$inc: {age:50},$set: {name: ' HoHo '}},false,true); equivalent: Update users set age= age +, name = ' HoHo ' WHERE name =' Lisi ';3, delete db.users.remove ({age:132});4, Query modification Delete db.users.findAndModify ({query: {age: {$gte: 25}}, Sort: {age:-1}, update: {$set: {name: ' A2 '},$inc: {Age:2}}, remove:true});d B.runcommand ({findandmodify:"Users", query: {age: {$gte: 25}}, Sort: {age:-1}, update: {$set: {name: ' A2 '},$inc: {Age:2}}, remove:true});

MongoDB Common Commands

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.