MongoDB Use guide-Basic operations _mongodb

Source: Internet
Author: User
Tags mongodb

Read
Db.collection.find ()

Db.users.find (
  {age: {$gt:}},
  {name:1, address:1}
). Limit (5). Sort ({age:1})

Users are collection names and are searched from users;

Age is the criteria for query, the result of which is the value of the Lookup name field is greater than 18;

Name is projection, the filter column (1 is present, 0 represents nonexistent), and represents the value in the returned result that contains the NAME,ADDRESS,_ID (Default include field) field;

Limit is cursor modifier, the representative returns up to 5;

Sort is 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 the sort is used;

Db.collection.findOne () is the return of a value.

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

Insert
Db.collection.insert ()

Db.users.insert (
  {
    name: "Sue",
    age:22,
    Status: "A"
  }
)

Note:

1. If there is no _id property in the INSERT statement, Mongod automatically adds a _id property and produces a unique value, ObjectId.

2. If you have the _id property in the INSERT statement, you must ensure that its value is unique and, if repeated, returns an exception.

3. 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 argument to True, you can update all compliant documents

2. The Save method can only update a single document

Delete
Db.collection.remove ()

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

Note:

1. By default, the Remove method deletes 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.