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:
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