MongoDB shell operations

Source: Internet
Author: User

The shell command operation syntax is similar to that of JavaScript. In fact, the underlying query statements on the console are all completed using JavaScript scripts. Run the shell command to start cmd.exe.

Common shell commands are as follows:

1. query the names of all local databases

> show dbs;

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

> use mydb;

3. query all collection sets in the current database (equivalent to table)

> show collections;

4. Create a collection

> db.createCollection('mycollection');

5. query the number of data entries in the aggregation set

> db.mycollection.count();

6. insert data

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

Insert a number Library to the 'mycollection' collection. The name is 'xyz _ lmn ', the age is '26', and the salary is '123'

7. query data with age equal to 26

> db.mycollection.find({"age":26});

8. query data with salary greater than 100

> db.mycollection.find({salary:{$gt:100}});

9. query data with age less than 30 and salary greater than 100

> db.mycollection.find({age:{$lt:30}},{salary:{$gt:100}});

10. query data with salary less than 40 or salary greater than 200

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

11. query the data of a specified Column

> db.mycollection.find({},{age:1,salary:1});

1 indicates the meaning of the column, or true.
12. query data whose username contains 'E'

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

13. query data headers with a header

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

14. query age column data and remove duplicate data

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

15. query the first 10 data records

> db.mycollection.find().limit(10);

16. query all data after one record

> db.mycollection.find().skip(1);

17. query the first data entry.

> db.mycollection.findOne();

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

db.mycollection.find({$or: [{salary: {$lt:40}}, {salary: {$gt:100}}]}).count();

19. sort by salary in ascending order

> db.mycollection.find().sort({salary:1});

Sort by salary field in ascending order

20. Descending Order

> db.mycollection.find().sort({salary:-1});

Sort by salary field in descending order

21. Modify the age according to Username

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

DB. collection. Update (criteria, objnew, upsert, multi)

Criteria: Query condition for update, similar to
Objnew: The update object and some updated operators (such as $, $ Inc...) can also be understood as
Upsert: If no update record exists, whether to insert objnew. True indicates insertion. The default value is false.
Multi: the default value of mongodb is false. Only the first record found is updated. If this parameter is set to true, all the records identified by the condition are updated.

22. Add the age field of the specified username to 5

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

Add the age field with username 'Jim 'to 5
23. delete data whose username is 'Rose'

> db.mycollection.remove({uname:'rose'});

24. Rename collection

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

Rename the mycollection set to 'C _ temp'

25. delete a set

> db.c_temp.drop();

Delete A set named 'C _ temp'

26. Delete the current database

> db.dropDatabase();


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.