One, insert operation
Insert operations are the basic method of inserting data, and using insert for the target collection will add the document to the MongoDB and automatically generate the corresponding ID key (MONGODB). The document structure takes a JSON-like Bson format. The common insert operation consists of a single insert and a bulk insert in two forms. The insertion is simply to save the document in the database without additional validation and code execution, so there is no possibility of an injection attack.
1, single insert
2. BULK INSERT
MongoDB support for bulk inserts is achieved by passing an array of multiple documents to the database. Because it inserts data by sending a TCP request, it simply sends a single TCP request, and the database does not have to handle a large number of headers to reduce the insertion time. In this way a bulk insert can only insert multiple documents into a single collection at a time, and it is possible to iterate through the insert operation for inserting into multiple collections.
Two, remove operation
The Remove function can be used to delete data, it can accept a document as an optional parameter, and only documents that are eligible are deleted. Deleting data is permanent, cannot be undone, cannot be recovered, and requires caution. Deleting a document requires emptying the entire collection, as well as deleting the collection directly.
Third, update operation
The update function is used to modify the data in the database, it receives two parameters, one is the query document, is used to find the document to be updated, and the other is the modifier document, which describes the changes to the found document. The update operation is atomic, and if multiple updates occur at the same time, all updates are executed, but the final update is the final winner.
1, the overall update (document replacement)
2, local update (modifier)
Partial updates are implemented through the atomic update modifier, and any other value can be changed when the modifier is used except for the value of "_id". Document substitution is a way to change all values.
$inc modifier: Adds a specific step size to the value of the specified property and creates it if the key does not exist.
$set modifier: A value that specifies a key and creates it if it does not exist.
$push: Array modifier, if the specified key exists, adds an element to the end of the existing array, and the key does not exist, a new array is created.
3, Upsert operation
The Upsert operation has saveorupdate functionality, and if no document meets the update criteria, create a new document based on the update criteria and update the document. If there is a document that meets the update criteria, the update is normal. When a new document is created, it is based on a conditional document, which acts on top of the modifier. The Upsert operation is atomic and efficient.
4, Batch Update
A batch update simply sets the 4th parameter of update to TRUE.
Four, instantaneous completion
The above inserts, deletes, and updates are all instantaneous, and they do not need to wait for the database to respond. This implementation can achieve high performance, very fast, only by the client send speed and network speed constraints. However, because the server status is not obtained, the operation is not guaranteed to be completed successfully. This is not feasible for a paid system with a higher security system, which requires a secure version of these operations. The safe version runs the GetLastError command immediately after the operation is executed to check for successful execution. If the failure typically throws a catch exception, then we can handle it in code.
V. Requests and connections
The database creates a queue for each MongoDB database connection, a request to hold the connection, and a new request from the client that is placed at the end of the queue. Subsequent requests are executed only if the requests in the queue have been completed. That is, for a single connection, the request is sequential and does not have a concurrency problem, so it always reads what it writes. However, there is a risk of read and write inconsistencies for different connections, and this behavior is particularly noticeable when the driver uses connection pooling. Specific connection pool information can refer to the official website: MongoDB official website