Create, update, and delete documents. Read---mongodb authoritative guide.

Source: Internet
Author: User
Tags bulk insert delete key modifier

Insert Document:

Db.foo.insert ({"Key": "Value"});

Inserting a data using insert will automatically add _id to the document if no _id is in the document.

Batchinsert (array) BULK INSERT, accept a document array to do parameters.

Use BULK Insert maximum length of 48M, if one fails in the process, succeeds before failure, and then all fails.

You can set the ContinueOnError option setting to ignore the error and continue inserting.

Insert Check: All documents must be less than 16M.

To delete a document:

Db.foo.remove ();

The above command deletes all the documents in the Foo collection, does not delete the collection itself, and does not delete the collection's meta information. Remove can accept a query document as an optional parameter.

Deletions are permanent, cannot be undone, and cannot be recovered.

Deleting a document is usually quick, but it is faster to clear the entire collection, delete the collection directly using drop, and then rebuild the indexes from the empty collection.

To update a document:

Db.user.update (parameter 1, parameter 2)

The first parameter queries the document, and the second parameter modifies the document.

Common errors, asking documents to match multiple documents, updating when the second parameter produces a reread _id value, the database throws an error. No documents are updated.

Using a _id query is faster than using a random field because the index is established by _ID.

It is best to use a modifier when adding a modifier or delete key to avoid replacing the original document.

Using modifiers:

$set: Used to specify the value of a field, and if the field does not exist, it is created. $set can modify the type of the key. ----> $unset Delete a key.

$inc: Used to increase the value of an existing key and create one if it does not exist. $inc can only manipulate integers, long integers, or double-precision floating-point values. The value of the $inc must be a number.

$push: Adds an element to the end of the array and creates a new array if it is not.

$each: Iterating through an array, you can add multiple values with the $push operation.

$slice: Sets the maximum length of the array, $slice value must be a negative integer. The combination of $slice and $push guarantees that the array does not exceed the maximum length set.

$sort: Cleaning up data

$ne: guarantees that the elements within the array are not duplicated.

$addToSet: If the document does not exist, add it.

$pop: You can delete an element from either end of the array. {"$pop": {"key": 1}} is removed from the end of the array. {"$pop": {"key":-1}} is removed from the array header.

$pull: Deletes an element based on a specific condition. $pull will tell you all the files that were matched to the deletion.

$: Location-based array modifier. Used to locate and update the array elements that the query document already matches.

Db.blog.update ({"Comments.author": "Jhon"},

... {"$set": {"Comments.$.author": "Jim"}})

 Upsert: If you do not find a document that meets the update criteria, create a new document with the conditional document and update the document.

By default, update intelligence performs actions on the first document that meets the criteria. If more than one document meets the criteria, only the first document is updated back. If you match all documents, you need to set the update fourth parameter to TRUE.

Findandmodify: Returns the matching result in one artificial and updates it.

Optional fields:

Findandmodify: String, collection name.

Query: Queries the document.

Sort: The result condition of the sort.

Update: the modifier document.

Remove: A Boolean type that indicates whether the document is deleted.

NEW: Boolean type that indicates whether to return the document before the update or the updated document. The default is the pre-update document.

Fields: The field that needs to be returned in the document.

Upsert: Boolean type, when True indicates that this is a upsert default is False.

  

  

  

Create, update, and delete documents. Read---mongodb authoritative guide.

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.