Ruby Operation MongoDB (advanced nine)-bulk operation Bulk Operations

Source: Internet
Author: User

This article mainly explains the knowledge of batch operation. The following methods are mainly explained using Insert_one,insert_many,delete_one,delete_many,replace_one,update_one,update_many seven methods.

The bulk-write API will want the server to send a list of actions in a single method call. If you set the ordered parameter, you can ensure that the order in which you pass the operation is executed.

The bulk write method has three parameters: the first is a list of actions, the second is the ordered parameter of the Boolean type, the default is true, and the third is the write correlation parameter, which is related to the collection write by default.

The following shows a valid write operation:

    1. Insert_one method

{: Insert_one=>{:x=>1}}

2. Insert_many method

{: insert_many=>[{:x=>1},{:x=>2}]}

3. Delete_one method

{:d elete_one=>{:filter=>{:x=>1}}}

4. Delete_many method

{:d elete_many=>{:filter=>{:x=>1}}}

5. Replace_one method

{: Replace_one=>{:filter=>{:x=>1}},: Replacement={:x=>2},: Upsert=>true #可选参数, The default is False}

6. Update_One method

{: Update_one=>{:filter=>{:x=>1},: update=>{' $set ' =>{:x=>2}},: Upsert=>tru e}}

7. Update_many method

{: Update_many=>{:filter=>{:x=>1},: update=>{' $set ' =>{:x=>2}},: Upsert=>tr UE}}

The following example shows how to pass an operation to a bulk write method

coll=client[' Documents ']coll.bulk_write ([{:insert_one=>{:x=>1}},                  {:update_one=>{:filter=>{:x=>1},                                  :update=>{' $set ' =>{: x=>2}}}                  },                  {:replace_one=>{:filter=>{:x=>2},                                   :replacement=>{:x=>3}                                   }}                              ],:order=>true)

This concludes the learning of bulk operations

This article from "Techfuture" blog, declined reprint!

Ruby Operation MongoDB (advanced nine)-bulk operation Bulk Operations

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.