MongoDB Shell operation

Source: Internet
Author: User

Shell command manipulation syntax is similar to JavaScript, in fact, the bottom of the console query statements are done with JavaScript script. With the shell command, you need to start Mongo.exe.

Common shell commands are as follows:

1. Querying all local database names

> Show DBS;

2. Switch to the specified database environment (if no database is specified, create a new library)

> Use mydb;

3. Query all clustered collections under the current library collection (equivalent to table)

> Show Collections;

4. Create a Clustered collection

> db.createcollection (' mycollection ');

5. Query the number of data bars in the aggregation collection

> Db.mycollection.count ();

6. Inserting data

> Db.mycollection.insert ({' username ': ' xyz_lmn ', ' age ': +, ' salary ': 120});

Insert a number library into the ' mycollection ' aggregation set, name ' Xyz_lmn ', age ' 120 ', salary

7. Query for data with age equal to 26

> Db.mycollection.find ({"Age":+});

8, query salary more than 100 of the data

> Db.mycollection.find ({salary:{$gt:+});

9, query age less than 30,salary more than 100 of data

> Db.mycollection.find ({age:{$lt:}},{salary:{$gt: +}});

10, query salary less than 40 or salary greater than 200 of the data

> Db.mycollection.find ({$or: [{salary: {$lt: +}}, {salary: {$gt: 200}}]});

11, query the data of the specified column

> Db.mycollection.find ({},{age:1, Salary:1});

1 indicates the meaning of this column, or it can be expressed as true
12, query username contains ' e ' data

> Db.mycollection.find ({username:/e/});

13. Query the data that starts with a

> Db.mycollection.find ({username:/^a/});

14. Query The Age column data and remove duplicate data

> db.mycollection.distinct (' Age ');

15, query the first 10 data

> Db.mycollection.find (). Limit (n);

16, query all the data after 1

> Db.mycollection.find (). Skip (1);

17. Query the first piece of data

> Db.mycollection.findOne ();

18. Number of records in query result set (query salary less than 40 or greater than 100)

Db.mycollection.find ({$or: [{salary: {$lt: +}}, {salary: {$gt: +}]}). Count ();

19. Sort by Salary ascending

> Db.mycollection.find (). Sort ({salary:1});

Sort by salary word orderby order

20, descending

> Db.mycollection.find (). Sort ({salary:-1});

Sort Descending by Salary field

21. Modify age According to username

> db.employee.update ({username: ' jim '},{$set: {age:}},false,true);

Db.collection.update (criteria, objnew, Upsert, Multi)

Criteria:update query conditions, similar to those in the SQL update query
Objnew:update objects and some updated operators (such as $, $inc ... ) can also be understood as the SQL update query within the set after the
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.

22. Increase the age field of the specified username by 5

> db.mycollection.update ({username: ' jim '},{$inc: {age:5}},false,true);

Add 5 to the Age field of ' Jim ' for username
23. Delete username as ' rose ' data

> Db.mycollection.remove ({uname: ' Rose '});


24. Set Collection Rename

> db.mycollection.renameCollection (' c_temp ');

Rename the MyCollection collection to ' C_temp '

25. Deleting a collection

> Db.c_temp.drop ();

Delete a collection named ' C_temp '

26. Delete the current database

> Db.dropdatabase ();

MongoDB Shell operation

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.