MongoDB command example

Source: Internet
Author: User
Use the example of a book to describe how to use the mongodb command. Add a library document: db. books. insert ({name: learn more about MongoDB}); then, you can check whether the insert is successful: db. books. find (); the printed results are similar to the following: {_ id: ObjectId (4f8e8a8e7a919fd8a1a37e2d), name: Deep Learning Mo

Use the example of a book to describe how to use the mongodb command. Add a library document: db. books. insert ({name: learn more about MongoDB}); then, you can check whether the insert is successful: db. books. find (); the printed results are similar to the following: {_ id: ObjectId (4f8e8a8e7a919fd8a1a37e2d), name: Deep Learning Mo

Use the example of a book to describe how to use the mongodb command.

Add

Insert a book document:

Db. books. insert ({name: 'learn MongoDB in depth '});

Then, you can check whether the insertion is successful:

Db. books. find ();

The printed results are similar to the following:

{"_ Id": ObjectId ("4f8e8a8e7a919fd8a1a37e2d"), "name": "Learn MongoDB in depth "}

Now, you don't want this record. Delete it:

Db. books. remove ();

If you use find () Again, the content will not be printed. Note that remove () will delete all documents in the collection. How to delete a document with a specified condition will be discussed later.

Modify

Create a new library document:

Db. books. insert ({name: 'History of money ', items: []});

Here I have added an empty array, which will be useful later.

Now I want to change the book name to: learn more about MongoDB.

Command:

Db. books. update ({_ id: ObjectId ("4f8e8a8e7a919fd8a1a37e2d")}, {name: 'learn MongoDB in depth '}, false );

The disadvantage of the preceding command is that the items attribute is missing. If you only want to modify the name of a book without affecting other attributes of the document, you can do this:

Db. books. update ({_ id: ObjectId ("4f8ea9a619bc948142e0dad5") },{$ set: {name: 'Deep in MongoDB '}}, false );

Now, I want to add something to an empty array, for example, an entry:

{_ Id: ObjectId, type: 'Chapter ', items: []}

The command is as follows:

? Db. books. update ({_ id: ObjectId ('4f8ea9a619bc948142e0dad5')}, {$ push: {items: {_ id: new ObjectId (), type: 'Chapter ', items: [] }}, false );

Let's take a look at the result:

{
"_ Id": ObjectId ("4f8ea9a619bc948142e0dad5 ″),
"Items ":[
{
"_ Id": ObjectId ("4f8eaf227a919fd8a1a37e2e "),
"Type": "chapter ",
"Items": []
}
],
"Name": "wild food"
}

Now we want to add content to the array of entries in 4f8eaf227a919fd8a1a37e2e. How can we do this? To use push, you can append an element to the specified array:

Db. books. update ({_ id: ObjectId ('4f8ea9a619bc948142e0dad5')}, {$ push: {items: {_ id: new ObjectId (), type: 'Chapter ', items: [] }}, false );

Now I find that the type value of the appended element is incorrect. I want to change it to section:

Db. books. update ({_ id: ObjectId ("4f8ea9a619bc948142e0dad5"), 'items. _ id': ObjectId ("4f8ef10b7a919fd8a1a37e32")}, {$ set: {'items. $. type ': 'region'}, false );

Can I append an element to the items array of the items element? The answer is yes:

Db. books. update ({_ id: ObjectId ("4f8ea9a619bc948142e0dad5"), 'items. _ id': ObjectId ("4f8ef10b7a919fd8a1a37e32")}, {$ push: {'items. $. items ': {_ id: new ObjectId (), type: 'core', items: [] }}, false );

The append result is similar to the following:

{
"_ Id": ObjectId ("4f8ea9a619bc948142e0dad5 ″),
"Items ":[
{
"_ Id": ObjectId ("4f8ef10b7a919fd8a1a37e32 "),
"Items ":[
{
"_ Id": ObjectId ("4f8ef4d17a919fd8a1a37e33 "),
"Type": "section ",
"Items": []
}
],
"Type": "chapter"
}
]
}

Delete

If you want to delete an attribute that is not added to the document, for example:

Db. books. update ({_ id: ObjectId ('4f8e8a8e7a919fd8a1a37e2d ')}, {$ push: {hello: 'test1' }}, false );

Adds a useless attribute for the document, he l lo, which is an array.

Delete it:

Db. books. update ({_ id: ObjectId ('4f8e8a8e7a919fd8a1a37e2d ') },{$ unset: {hello: 1 }}, false );

Original article address: MongoDB command usage example. Thank you for sharing it.

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.