[MongoDB] Learning notes-basic operations

Source: Internet
Author: User

Read

Db.collection.find ()

db.users.find (    }},     1 1 }). Limit (5). Sort ({age:1})

Users are collection names and are searched from users;

The age is the query criteria, filtering results, representing the value of the Lookup name field is greater than 18;

Name is projection, the filter column (1 for presence, 0 for non-existent), represents the value of the name,address,_id (default included field) field in the returned result;

Limit is the cursor modifier, which represents a maximum of 5 returns;

Sort is sort, sorted by the value of age.

Note:

1. All inquiries can only be directed to a single collection;

2. The query results are not sorted unless you use sort;

Db.collection.findOne () is a value returned.

Db.collection.find () returns the cursor type.

Insert

Db.collection.insert ()

Db.users. Insert (    {        name: "Sue", age        :$,        Status: "A"    })

Note:

1. If the INSERT statement does not have the _id property, Mongod automatically adds a _id property and produces a unique value, ObjectId.

2. If the INSERT statement has the _id attribute, it must be guaranteed that its value is unique and, if repeated, an exception is returned.

3. The update () and save () operations can also implement the Insert function.

Update

Db.collection.update () or Db.collection.save ()

Db.users. Update (    {age:{$gt:}},                --Update criteria    {$set: {status: ' A '} },         --Update action    {multi:true}                      --update option)

Note:

1. By default, the Update method updates only one document, but if you set the multi parameter to True, you can update all documents that are compliant

2. The Save method can only update one single document

Delete

Db.collection.remove ()

Db.users.remove (    {status: "D"}        --Remove criteria)

Note:

1. By default, the Remove method removes all documents that meet the criteria

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.