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