MongoDB Learning Notes ~ Uncle Share Bulk Add-bulk Update-Bulk Delete

Source: Internet
Author: User

Back to Catalog

Say it is a bulk operation, that is, to commit the collection object to the server one at a time, and persist the data , if your code is a one-time commit, it is not counted as a bulk operation! In the previous MongoDB warehousing did not implement the bulk update and bulk deletion, and today in the project, or the implementation of this batch of operations, and has passed the test, the following public source code

 Public voidInsert (ienumerable<tentity>Item) {            varList =NewList<writemodel<tentity>>(); foreach(varIIteminchItem) {list. ADD (NewInsertonemodel<tentity>(IItem)); } _table. Bulkwriteasync (list).        Wait (); }         Public voidUpdate (ienumerable<tentity>Item) {            varList =NewList<writemodel<tentity>>(); foreach(varIIteminchItem) {querydocument querydocument=NewQuerydocument ("_id",NewObjectId (typeof(TEntity). GetProperty (EntityKey). GetValue (IItem).                ToString ())); List. ADD (NewUpdateonemodel<tentity> (Querydocument, builders<tentity>. Update.combine (Generatormongoupdate (IItem))); } _table. Bulkwriteasync (list).        Wait (); }         Public voidDelete (ienumerable<tentity>Item) {            varList =NewList<writemodel<tentity>>(); foreach(varIIteminchItem) {querydocument querydocument=NewQuerydocument ("_id",NewObjectId (typeof(TEntity). GetProperty (EntityKey). GetValue (IItem).                ToString ())); List. ADD (NewDeleteonemodel<tentity>(querydocument)); } _table. Bulkwriteasync (list).        Wait (); }

In the implementation of the program, we used the Writemodel generic object, which will store the objects to be inserted, updated and deleted, and for the insert, only one parameter is its entity collection, and the object update, it not only has the entity collection but also has the corresponding condition querydocument, And for the delete operation, only provide querydocument!

Back to Catalog

MongoDB Learning Notes ~ Uncle Share Bulk Add-bulk Update-Bulk Delete

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.