MongoDB Learning (3) MongoDB shell command line usage

Source: Internet
Author: User

First, start the MongoDB shelltool, that is, bind the 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 dbtest;

Switch to the dbtest database or create a database named dbtest.
3. query all collection sets in the current database (equivalent to table)

> show collections;

4. Create a collection

> db.createCollection('employee');

Creates an aggregation set named 'Employee '.
5. Insert data

> db.employee.insert({'uname':'teddy','age':24,'salary':11000});

Insert a number Library to the 'Employee' aggregation set. The name is 'Teddy ', the age is '24', and the salary is '123'

6. query the number of data entries in a collection.

> db.employee.count();

7. query data whose age is 23

> db.employee.find({"age":23});

8. query data with salary greater than 5000

> db.employee.find({salary:{$gt:5000}});

9. query data with age less than 23 and salary greater than 8000

> db.employee.find({age:{$lt:24}},{salary:{$gt:8000}});

10. query data with salary less than 4000 or salary greater than 20000

> db.employee.find({$or: [{salary: {$lt:4000}}, {salary: {$gt:20000}}]});

11. query the data of a specified Column

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

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

> db.employee.find({uname:/e/});

13. query data headers with a header

> db.employee.find({uname:/^a/});

14. query age column data and remove duplicate data

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

15. query the first 10 data records

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

16. query all data after one record

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

17. query the first data entry.

> db.employee.findOne();

18. Number of records in the query result set (query the number of records with salary less than 4000 or greater than 10000)

db.employee.find({$or: [{salary: {$lt:4000}}, {salary: {$gt:10000}}]}).count();

19. sort by salary in ascending order

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

Sort by salary field in ascending order

20. Descending Order

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

Sort by salary field in descending order

21. Modify the age according to the uname

> db.employee.update({uname:'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. Increase the age field of the specified uname by 5

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

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

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

24. Rename collection

> db.employee.renameCollection('t_emp');

Rename the employee set to 't_ emp'

25. delete a set

> db.emp_test.drop();

Delete A set named 'emp_test'

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.