MongoDB collection document creation, modification, deletion, and query command summary, mongodb modification and Deletion

Source: Internet
Author: User
Tags install mongodb mongodb collection

MongoDB collection document creation, modification, deletion, and query command summary, mongodb modification and Deletion

Install mongodb in windows. Start it. Previous Article: mongoDB Installation Details


1. log on to view the collection documents in the database to add, modify, and delete documents.

1. Check which databases can be used:

Show dbs;

2. view the name of the currently used database:

Db. getName ();

3. Using a database can be converted between databases like mysql.

Use dbname;

4. If there is no database, create a database. mongodb does not provide statements for creating a database like mysql but has similar functions: if this database exists, use this database. If it does not exist, create this database.

Use dbname;

Then perform some operations, such as db. createCollection ("collectionName ")

// Create the dbname database and a set

5. Create a set in the current database:

Example: db. createCollection ('collectionname ');

6. View All sets in the current database:

Db. getCollectionNames ();

7. Obtain a specified set of the current database:
Db. getCollection ("collectionName ");

8. add data to this set: Use the insert () function.

Db. collectionName. insert ({}); note that the data format used by mongodb is bson. If you know jsonObject, you will find that these two formats are similar:

The BSON format is specially developed for MongoDB and is similar to a json binary format.

This format is not necessarily smaller than the file stored in json. Its advantage is that the explanation is faster.

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

9. view the documents in this set: Use the find () function.

Example: db. mytest. find ();

// Query all documents in the Set mytest

10. Conditional Query

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

// Query documents with name key and name1 Value

11. Multi-condition query:

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

// Query the document whose name is name1age 2

12. distinct query:

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

// Query all documents from the set mytest, but the name key value cannot be repeated.

13. The query is smaller than a value or a university value.

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

// Query the document of age <2 in mytest.

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

// Query the document of age> 2 in the mytest set

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

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

// Query the document of age <= 2 in the mytest set

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

// Query the document with age> = 2 in the mytest set

15. Between two numbers

Example: db. mytest. find ({age: {$ lt: 6, $ gt: 2 }})

// Query the document of age <6 and age> 2 in the mytest set

16. sort query: Use the sort () function for sorting.

Example: db. mytest. find (). sort ({age: 1 })

// Sort by age in ascending order

Example: db. mytest. find (). sort ({age:-1 })

// Sort by age in descending order

// Query in ascending order

Example: db. mytest. find ({name: 'name1'}). sort ({age: 1 })

 

17. query the previous specified documents: Use the limit () function

Example: db. mytest. find (). limit (3)

// Query the first three documents in the mytest set

18. in and notin queries: $ in $ nin

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

// Query documents whose age is 2, 3, or 4 from the mytest set is similar to that of mysql in.

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

// Query documents whose age is not 2, 3, and not 4 from the mytest set

19. or query $ or

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

// Query documents whose name is name1 or age is 3

20. query the number of data entries in the Set:

Example: db. mytest. count ();

21. delete a document in the set or use the remove () function for all documents.

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

// Delete the document with name key and value name1 from the mytes set

Example: db. mytest. remove ()

// Delete all documents in the mytest set

22. delete a set: Use the drop () function.

Example: db. mytest. drop ()

// Delete the set mytest

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

Example: db. dropDatabase ();

 

2. Modifier

1. Use the update command to replace an archive.

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

// Replace the document named name1 in mytest with the following

// There are two parameters in update. The first parameter is the document to be replaced, and the second parameter is the document to be replaced.

2. It is very troublesome to modify it, so we need to use the $ set modifier to modify it:

Example: db. mytest. update ({name: 'name', {$ set: {name: 'name2 '}}})

// Modify the file name: 'name2' in the mytest set'

// The $ set modifier is different from the update modifier. The first parameter is used to locate the file, and the second parameter is the attribute field to be modified, if not, this key-value pair is added. If yes, its value is modified.

 

3. If yes, the specified order of magnitude is increased. If no value exists, the key-value pair is added:

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

// Search for the document whose key value is name: name1 in the Set "mytest". If the document contains the age key, add 3 to the original value. If not, add this key-value pair.

4. array modifier. If this array exists, add the value at the end of the array. If this array does not exist, create an array:

Example: db. mytest. update ({name: 'name1'}, {$ push: {comment: {name: 'cname', age: 6, comment: 'good '}}})

// Find the document with the key value of name: 'name1' in the mytest set, and then add a comment array to this document. The content in the array includes name, age, and comment.

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

// During the second operation, the comment set already exists, so these key-value pairs will be added to the end of the comment array as an element of data.

 

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: 'A @ 163.com '}})

// Add an email array to this document whose key value is name: name1 in the mytest set through addToSet. If there is an array, it will not be added again. If not, it will be added.

6. add multiple values to the array by combining addToSet and each:

Example: db. mytest. update ({name: 'name1'}, {$ addToSet: {email: {$ each: ['a @. com ',' B @. com ', 'c @. com '] }})

// Add multiple values to the email array at a time through each Traversal


Note: here we emphasize that most bson formats are used in mongodb. You will also find that we use insert (), find () remove () and other function parameters are written in bson format.

 

Iii. BSON knowledge

Brief Introduction to BSON:

BSON is a type of json binary storage format, referred to as BinaryJSON. Like JSON, BSON supports embedded document objects and array objects, however, BSON has some data types not available in JSON, such as Date and BinData.

BSON can be used as a storage mode for network data exchange. This is a bit similar to Google's ProtocolBuffer, but BSON is a schema-less storage mode, which has the advantages of high flexibility, however, its disadvantage is that the space utilization is not ideal,

BSON has three features: lightweight, reproducible, and efficient.

 

Format comparison:

(1), bson: {key: value}

(2), json: {key: value}

(3) Map: {key = value}

 

 

 

 

 

 

 

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.