Detailed description of MongoDB database insertion, update, and deletion operations

Source: Internet
Author: User
1. Insert is the basic method for MongoDB to Insert data. If you use Insert for the target set, this document is added to MongoDB and the corresponding ID key is automatically generated. The document structure adopts the JSON-like BSON type. Common insert operations include single insert and batch insert. During insertion, the document is simply stored in the database.

1. Insert is the basic method for MongoDB to Insert data. If you use Insert for the target set, this document is added to MongoDB and the corresponding ID key is automatically generated. The document structure adopts the JSON-like BSON type. Common insert operations include single insert and batch insert. During insertion, the document is simply stored in the database.

1. Insert operation

The Insert operation is the basic method for MongoDB to Insert data. If you use the Insert operation on the target set, this document is added to MongoDB and the corresponding ID key is automatically generated. The document structure adopts BSON format similar to JSON. Common insert operations include single insert and batch insert. During insertion, the document is simply stored in the database without additional verification or code execution. Therefore, there is no possibility of injection attacks.

1. Single insert


2. Batch insert

MongoDB supports batch insert by passing an array composed of multiple documents to the database. Because it inserts data by sending TCP requests, it only needs to send a single TCP request, and the database does not need to process a large number of message headers, reducing the insertion time. In this way, you can Insert multiple documents into one set at a time. You can call the Insert operation cyclically when inserting documents into multiple sets.


Ii. Remove operation

The remove function can be used to delete data. It can take a document as an optional parameter and only documents that meet the criteria will be deleted. Deleting data is permanent and cannot be undone or restored. Therefore, be cautious. To delete a document, you need to clear the entire set. It is better to delete the set directly.

Iii. Update operations

The update function is used to modify the data in the database. It receives two parameters. One is the query document, which is used to find the document to be updated, and the other is the modifier document, which describes the modifications made to the found document. The update operation is atomic. If multiple updates occur at the same time, all updates will be executed, but the last update is the final winner.

1. Overall Update (Document Replacement)


2. Partial Update (modifier)

Some updates are implemented through the atomic update modifier. When using the modifier, the value of "_ id" cannot be changed, and any other value can be changed. Document Replacement can change all values.

$ Inc modifier: adds a specific step to the value of a specified attribute. If the key does not exist, it is created.


$ Set modifier: used to specify a key value. If it does not exist, it is created.


$ Push: array modifier. If the specified key exists, an element is added to the end of the existing array. If the key does not exist, a new array is created.


3. upsert operation

The upsert operation has the saveOrUpdate function. If no document meets the update conditions, a new document is created based on the Update Conditions and the update documents. If there are documents that meet the update conditions, they will be updated normally. When creating a new document, the conditional document is used as the basis to apply the modifier to it. The upsert operation is atomic and efficient.


4. Batch update

For batch update, set the 4th parameters of update to true.

Iv. instant completion

The above insert, delete, and update operations are completed in an instant, and they do not need to wait for the database response. This implementation can achieve high performance and high speed, which is limited by the client's transmission speed and network speed. However, since the server status is not obtained, the operation cannot be completed smoothly. This is not feasible for paying systems with high security. In this case, you need to use their secure versions for these operations. The Security version immediately runs the getLastError command after the operation is executed to check whether the execution is successful. If a failure occurs, an exception that can be caught is thrown, which can be processed in the code.

5. Requests and connections

The database creates a queue for each MongoDB database connection to store the connection requests. The new requests sent by the client are placed at the end of the queue. Only requests in the queue are executed, and subsequent requests are executed. That is, for a single connection, requests are executed sequentially without concurrency issues, so it can always read what it writes. However, reading and writing may be inconsistent between different connections. Pay special attention to this behavior when the driver uses the connection pool. For details about the connection pool, refer to the official website.

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.