MongoDB Bulk Operation MethodsThe MongoDB bulk methods are used to perform bulk operation like bulk write and bulk remove. #1. db.collection.initializeOrderBulkOp()The initializeOrderBulkOp and gives a new Bulk() operations builder for the collection. It constructs an ordered list of write operations that MongoDB runs in bulk. he following initializes a Bulk() operations builder on the users collection, adds a series of write operations, and executes the operations: #2. db.collection.initializeUnorderedBulkOp()The method boot up and gives a new Bulk() operations builder for the collection. It constructs an unordered list of write operations that MongoDB runs in bulk. For Example: The following initializes a Bulk() operations builder and adds a series of insert operations to add multiple documents: #3. Bulk() methodBulk method can be used to create a list of write operations to perform in bulk for one collection. To obejectified the builder, use either the The Bulk() builder has the following methods:
#4. Bulk.execute() methodIt runs the list of operations that is built by the Bulk() methods builder. For example: Executing Bulk method The following initializes a Bulk() operations builder on the items collection, adds a series of insert operations, and executes the operations: #5. Bulk.find(<query>) methodIt can be used to specify the query condition for an update or a remove operation. Specifies a query condition using Query Selectors to select documents for an update or a remove operation. To specify all documents, use an empty document {}. Bulk.find() can be used with the following operations of write command: For example: The examples given below initializes a Bulk() operations builder for the items collection and adds a remove operation. It also update the operation to the list of operations. Remove operation and update operation with the help Bulk.find() method, specifies a condition for their respective actions: #6. Bulk.find.remove() methodIt adds a remove operation to a bulk operations list. The Bulk.find() method can be used to specify the condition, which determines the documents to be remove. The Bulk.find.remove() removes all matching documents from the collection. The example given below initializes a Bulk() operations builder for the items collection and adds remove operation. The remove operation can be used to remove all documents in the collection where the status equals "D": #7. Bulk.find.replaceOne(<document>) methodIt can be used to add a single document replacement operation to a bulk operations list. Use this method to specify the condition that determines the document that has to be replaced. The method limits the replacement to a single document. A replacement document that completely replaces the existing document. Contains only field and value pairs. For example The example given below initializes a Bulk() operations builder for the items collection, and adds various replaceOne operations to the list of operations. #8. Bulk.find.update(<update>) methodIt can be used to add a multi update operation to a bulk operation list. The method updates specific fields for the existing files. The Bulk.find() method can be used to specify the condition that determines the documents that has to be updated. The method updates all matching documents. For Example: The following example initializes a Bulk() operations builder for the items collection, and adds various multi update operations to the list of operations. #9. Bulk.insert(<document>) methodIt can be used to add an insert operation to a bulk operations list. For example: The example initializes a Bulk() operations builder for the items collection and adds a series of insert operations to add multiple documents: #10. Bulk.toString()It can be used to return the output as a string a JSON document that contains the number of operations and batches inside the Bulk() object. For example The example below initializes a Bulk() operations builder on the items collection, adds a series of write operations, and calls Bulk.toString() on the bulk builder object. Next TopicMongoDB Connection Methods |