"MongoDB" The basic operation of MongoDB, Insert\query\delete\update

Source: Internet
Author: User

1, Insert

MongoDB is database storing document object, the type of which is called Bson. (like JSON);

Example://Document Defination

Now after using Command[db.posts.insert (DOC), you'll insert record successfully if seeing the

The following picture.

2. Query

One of the fundamental functions of MongoDB is to support query dynamically, which is the same as the troditional relation Al database, but more effiecieny than.

2.1 Query Expression ojbects:

Query Expression Objects document is also a bson-structure document. For example, we could use the following the command to find all the record in the collections:

2.2 Query Item

In addition to Query expression object, Mongodb still support extra argument items. For example, the wanna return some certain fields. For example:

Example 1:return all the fields expect for tags;

Example 2:return Dall fields expect for comments and tags= "Albert"

Example 3:return the only field ' name ' and gender= ' male '


3. Remove

Removing operation is used-remove records from the collections. Example:


Advice:it ' s better to use _id as condition when executing the Remove operation/

Attention:in some conditions, when you were ready to remove a record, maybe in the meantime of the update operation is UPDA Ting this record which makes the reomve operation failed. You could add the $atomic field to avoid this case. For example:


4. Update4.1 Grammar


Argument Description:

Criteria:the object used to set query conditions

Objnew:object used to set update content.

Upsert:if record exists, it would update it. Else insert a new record.

Multi:if Multi matches Conditoin, it'll update all the records.

Attention:by fault, MongoDB would update the first record that matches the query condition.

"MongoDB" The basic operation of MongoDB, Insert\query\delete\update

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.