MongoDB Document BASIC Operations

Source: Internet
Author: User
Tags set set

First, insert the document

Insert a document into a collection using the Insert () or save () method

>db. Collection_name. Insert (document)

Detailed usage can refer to MongoDB Novice Tutorial

Second, find the document

Find () Displays all documents in the collection in an unstructured way

>db. Collection_name.find ()

To display all documents in a formatted manner

>db.col.find (). Pretty ()

Here, by the way, the conditional operator:

    • (>) Greater than-$GT
    • (<) less than-$lt
    • (>=) greater than or equal to-$gte
    • (<=) less than or equal to-$lte

This allows you to query the specified document by setting criteria, such as

})

That is, all documents with a value greater than 100 for the query field "likes"

For detailed usage, please refer to MongoDB Novice Tutorial

Third, update the document

MongoDB uses the update () and Save () methods to update the documents in the collection.

Update operation

Syntax format

Db.collection. Update (   <query>,   <update>,   {     <boolean>,     <boolean>  ,     <document>   })

Parameter description:

    • Query: The search condition for update, similar to where in SQL update query.
    • Update: Update object and some updated operators (such as $, $inc ... ) can also be understood as the SQL update query within the set after the
    • upsert : Optional, this parameter means that if there is no record of update, insert objnew,true as INSERT, default is False, do not insert.
    • multi : Optional, mongodb default is False, only update the first record found, if this parameter is true, will be found on the condition of a number of records update all.
    • writeconcern : Optional, throws an exception level.

Worth talking about is the update of the whole update and the local update, where the local update, MONGODB provides two modifiers, $inc and $set, as the name implies, $inc is to add the corresponding value of the field, and $set set the field directly to the corresponding value, for example

Db. user. Update ({"Name": "S"},{$inc: {"Age":5}) db. user. Update ({"Name": "S"},{$set: {"Age":5})

The age of users named "S" was increased by 5 and changed to 5 respectively.

Detailed usage Reference MONGODB rookie tutorial

Iv. Deleting documents

Syntax format

db.collection.remove (   <query>,   {     <boolean > ,      <document>   })

Parameter description:

    • query : (optional) The condition of the deleted document.
    • justone : (optional) If set to TRUE or 1, only one document is deleted.
    • Writeconcern : (optional) the level at which the exception is thrown.

If remove () takes no parameters, all data is deleted.

Detailed usage Reference MONGODB rookie tutorial

MongoDB Document BASIC Operations

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.