MongoDB Collection Document creation modify delete and Query command summary

Source: Internet
Author: User
Tags modifier mongodb collection

MongoDB installation under Windows, start to view previous: MongoDB installation detailed


I. Log in to view the collection document in the database database add a document , modify the document, delete the document

1. See which databases are available:

Show DBS;

2. View the name of the database currently in use:

Db.getname ();

3, using a database, as in MySQL can be converted between the database

Use dbname;

4, if there is no database to create a database, MongoDB does not provide a database like MySQL, such as the creation of statements but have similar functions of the command: if there is this database use this database if not, create this database

Use dbname;

Then perform some operations such as: Db.createcollection ("CollectionName")

This creates the DBName database and creates a collection

5. Create a collection in the current database:

Example: Db.createcollection (' CollectionName ');

6. View all the collections under the current database:

Db.getcollectionnames ();

7. Gets a specified collection of the current database:
Db.getcollection ("CollectionName");

8. Add data to this collection: Use the Insert () function

Db.collectionName.insert ({}); Note that MongoDB uses data in the format Bson if you know Jsonobject you will find that the two formats are similar:

Bson This format is a binary format that is developed specifically for MongoDB and is similar to JSON.

This format is not necessarily smaller than the JSON-stored file, and the advantage is that it is interpreted quickly.

Example: Db.mytest.insert ({name: ' name1 ', age:2})

9. See which documents are in this collection: using the Find () function

Example: Db.mytest.find ();

Query MyTest All documents in this collection

10. Conditional query

Example: Db.mytest.find ({name: ' name1 '});

Querying a document with the name key and a value of name1

11, Multi-criteria query:

Example: Db.mytest.find ({name: ' name1 ', age:2})

Querying for a document with the name Name1age 2

12, DISTINCT query:

Example: Db.mytest.distinct ("name")

All documents are queried from the MyTest collection, but the value of the name key cannot be duplicated

13. Query less than a certain value or a university value

Example: Db.mytest.find ({age:{$lt: 2}})

Query MyTest The AGE<2 document in this collection

Example: Db.mytest.find ({age:{$gt: 2}})

Querying age>2 documents in the MyTest collection

14, greater than or equal to, less than or equal to

Example: Db.mytest.find ({age:{$lte: 2}})

Querying age<=2 documents in the MyTest collection

Example: Db.mytest.find ({age:{$gte: 2}})

Querying age>=2 documents in the MyTest collection

15, between two numbers

Example: Db.mytest.find ({age:{$lt: 6, $GT: 2}})

Querying documents AGE<6 and AGE>2 in the MyTest collection

16. Sort query: Sort using the sort () function

Example: Db.mytest.find (). Sort ({age:1})

By age Ascending

Example: Db.mytest.find (). Sort ({age:-1})

By age Descending

An ascending query was made

Example: Db.mytest.find ({name: ' name1 '}). Sort ({age:1})

17. Query the first few specified documents: Use the limit () function

Example: Db.mytest.find (). Limit (3)

Querying the top three documents in a MyTest collection

18, In,notin query: $in $nin

Example: Db.mytest.find ({age:{$in: [2,3,4]}})

Querying a document with age 2 or 3 or 4 from the MyTest collection is similar to MySQL's in

Example: Db.mytest.find ({age:{$nin: [2,3,4]}})

Querying for documents with age not 2 and not 3 and not 4 from the MyTest collection

19, or query using $or

Example: Db.mytest.find ({$or: [{name: ' name1 '},{age:3}])

Querying for a document with name name1 or age 3

20. Query How many data are in this set:

Example: Db.mytest.count ();

21. Delete a document from the collection, or use the Remove () function for all documents

Example: Db.mytest.remove ({name: ' name1 '})

Removes the document with the name key and a value of name1 from the Mytes collection

Example: Db.mytest.remove ()

Delete all documents in the MyTest collection

22. Delete A collection: Use the drop () function

Example: Db.mytest.drop ()

Delete MyTest this collection

23. Delete the current database: Use the Dropdatabase () function

Example: Db.dropdatabase ();

second, the modified device

1. Replace an archive with the update command

Example: Db.mytest.update ({name: ' name1 '},{name: ' name1 ', age:2});

Replace the document with name Name1 in this collection with the following mytest

There are two parameters in update, the first is the document to be replaced, the second is the document to be replaced

2, so we want to modify the word is very troublesome, so we have to use the $set modifier to modify:

Example: Db.mytest.update ({name: ' name ', {$set: {name: ' Name2 '}}})

Modify the file name: ' Name ' in the MyTest collection name: ' Name2 '

$set modifier and update replace, his first argument is to find the file, the second parameter is to modify the attribute field, if not present, add this key value pair, if any, modify his value

3, if there is an increase in the specified order of magnitude, if not present, add a key value pair:

Example: Db.mytest.update ({name: ' name1 '},{$inc: {age:3}})

Look for a document with a key value of name:name1 in the MyTest collection, and add 3 to the original value if there is an age key in the document. If not, add the key value pair.

4, array modifier, if this array has the last add value in the array, if the array is not created array:

Example: Db.mytest.update ({name: ' name1 '},{$push: {comment:{name: ' CNAME ', age:6,comment: ' Good '}}})

In the MyTest collection, look for a document with the key value of name: ' Name1 ', and then add a comment array to this document, and the contents of the array include Name,age,comment

Db.mytest.update ({name: ' name1 '},{$push: {comment:{name:cname1,age:7,comment: ' Good2 '}}})

The second operation, comment the collection already exists, so they will be the key value pairs as an element of the data, added to the comment array of the last

5. Addtoset adds a value to the specified key, and the specified key is used as the array name:

Example: Db.mytest.update ({name: ' name1 '},{$addToSet: {email: ' [email protected] '}})

An e-mail array is added to this document with a key value of Name:name1 to the MyTest collection through Addtoset, and if there is one in the array, it will not be added again, if not.

6, Addtoset, and each combine to add multiple values to the array at once:

Example: Db.mytest.update ({name: ' name1 '},{$addToSet: {email:{$each: [' [email protected] ', ' [email protected] ', ' [email Protected]}})

Multiple values are added to the e-mail array one at a time through each traversal


Note: It is emphasized here that the Bson is used in MongoDB, and you will find that the parameters of the functions such as insert (), find () remove () are written in Bson format.

Third, Bson understand

Simply understand Bson:

Bson is a binary form of a class JSON storage format, called Binaryjson, which, like JSON, supports embedded document objects and array objects, but Bson has some data types that JSON does not have, such as date and bindata types.

Bson can be used as a form of storage for network data exchange, which is somewhat similar to Google's Protocolbuffer, but Bson is a kind of schema-less storage, it has the advantage of high flexibility, but its disadvantage is that space utilization is not ideal,

Bson has three features: light weight, ergodic, high efficiency

Format comparison:

(1), Bson: {key:value}

(2), json:{key:value}

(3), map:{key=value}

MongoDB Collection Document creation modify delete and Query command summary

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.