MongoDB additions and deletions to investigate the operation of detailed

Source: Internet
Author: User
Tags modifier

First, insert

The insertion of MongoDB is simple, using the Insert method, which demonstrates from creating a database, creating a collection to inserting a document, querying a document.

Collection Creation Method Parameter description:

Size: Collection Maximum space

Max: Maximum number of documents in a collection

( The oldest record will be deleted if the size and max limits are exceeded)

Second, delete

MongoDB Delete operation is also very simple, using the Remove method, here is a demonstration to delete the document just inserted.

If the Remove function does not pass arguments, the direct call is to delete all the documents in the collection.

Third, update

To facilitate the following demonstration, I have inserted 4 documents in the ' blog ' collection that I just created:

1. Replace the entire document (here is a demonstration to replace the first document):

(in this case the _id is not changed, if you want to update the _id , be careful to prevent key conflicts)

2. Use the modifier to update the document's specified section:

(1) $inc: Number increase / Decrease

(if the corresponding key does not exist the key will be created first, if the key corresponding value is not a number will be an error)

(2)$set: Set the key value

(You can manipulate a variety of data types, you can change the data type of the key value, you can also use to modify the embedded document, if the corresponding key does not exist, the key will be created first)

(3) $unset: Delete a key

(4) $push: If the corresponding key already exists, it will add an element to the end of the existing array, or a new array will be created.

(5) $addToSet: If a value is not inside the array, add it, and use the same method as the $push , but only the duplicate elements cannot be added.

will be $addToSet and the $each Together , you can add multiple different values to the array at once.

(6) $pop: Deletes an element from either end of the array.

(seemingly negative numbers are removed from the head, and 0 and positive numbers are removed from the end)

(7)$pull: Deletes all eligible elements in the array.

(8) Use array subscripts to select the array elements that need to be modified:

(9) Use the locator to select the array elements that you want to modify:

Modifier Speed:

$inc does not need to change the document size, so it is very fast.

The array modifier may change the document size, which is slower.

$set speed is faster when the document size does not change, otherwise performance will decrease.

MongoDB has reserved some padding for the document to accommodate the size change, but if it goes beyond the original space, a new space is allocated.

if $push become a bottleneck, you can separate the inline array into a single set.

(10)

MongoDB additions and deletions to investigate the operation of detailed

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.