MongoDB Add to delete operation

Source: Internet
Author: User
Tags mongodb add

(1) INSERT statement, INSERT statement is done by

1, when inserting data, do not need to create a pre-set (cus), insert data will be generated automatically.

2, a primary key (_ID) is created by default each time the data is inserted without a specified field (_id)

3,_ID the same value cannot be inserted repeatedly

For example: Db.cus.insert ({name: "Zhang San", Mobile: "11111111111", email: "[email protected]", mo:200});

(2) Modify statement syntax: Db.collection.update (query,update,<upsert>,<multi>)

Query: parameter is a search selector, value type document

Update: parameter is where you need to modify the value type is document if the update parameter contains only the field option and there is no operator, a substitution change occurs.

Upsert: Optional parameter, Boolen type, default is False, when value is true, the Update method updates the matching record and inserts a new record if no matching record is found.

Multi: Optional parameter, Boolen type, default is False, when the value is true, the Update method updates all matching records.

For example: Db.cus.update ({name: "Zhang San"},{$set: {name: "John Doe"}, $inc {mo:100}}) change the specified field name and increase the field Mo field by 100.

Db.cus.update ({name: "Zhang San"},{name: "John Doe"}) changes the specified field name and clears the collection (CUS) other fields except the _id, Name field.

Db.cus.update ({name: "Zhang San"},{$set: {name: "John Doe"}},{multi:true})//change all matching records

Db.cus.update ({name: "Zhang San"},{$set: {name: "John Doe"}},{upsert:true})//change matching record, insert record if no match is found

(3) DELETE statement Delete operation has remove operation completion syntax: Db.collection.remove (<query>,<justone>)

Query: Optional parameters, search criteria

Justone: Optional parameter, Boolean value that indicates whether to delete only the first record that matches the limit 1 condition in the relational database

1. Delete all matching documents Db.cus.remove ({name: "Zhang San"})

2. Delete the first document that matches Db.cus.remove ({name: "Zhang San"},1)

3, delete all documents, but do not delete index Db.cus.remove ()

When you delete a document with remove, the Document object is also deleted from the corresponding data file on the disk.

MongoDB Add to delete operation

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.