Insert Operation inserting records1. Insert a record Db.testData.insert ({num:1,name: ' a '}); result Writeresult ({"ninserted": 1})
2. View the inserted record db.testData.find ();
Insert Array 1. Define an array var arr = [{ Num:1,name: ' A '},{num:2,name: ' B '},{num:3,name: ' C '}];
2. Insert record db.testdata.insert (arr);
results ninserted display several data were inserted
Bulkwriteresult ({
" writeerrors ": [],
"writeconcernerrors": [],
"ninserted": 3,
"nupserted": 0,
"nmatched": 0,
"nmodified": 0,
"nremoved": 0,
"upserted": []
})
Bulk INSERT multiple Data Bulk is a new feature of 2.6
1. Initialize the batch operation constructor for the specified collectionvar bulk = Db.testData.initializeUnorderedBulkOp ();Initializing an unordered batch operation
This operation returns an unordered operation constructor that maintains the list of actions. The unsorted meaning is that MongoDB can synchronously perform multiple operations without order.
If an error occurs during the period, subsequent operations continue.
You can also construct an orderly operation constructor to view the Db.collection.initializeOrderedBulkOp ();
2. Add the insert operation to the bulk object
Bulk.insert ({num:4,name: ' d '});
Bulk.insert ({num:5,name: ' e '});
3. Performing batch Processing
Bulk.execute ();
Returns the result of the batch, ninserted represents a few inserts, and if an error occurs during the period, it is included in the result.
Bulkwriteresult ({
"Writeerrors": [],
"Writeconcernerrors": [],
"Ninserted": 2,
"nupserted": 0,
"nmatched": 0,
"Nmodified": 0,
"Nremoved": 0,
"upserted": []
})
Additional examples and methods db.collection.update () method,
Db.collection.findAndModify (),
Db.collection.save ()
These methods can also perform insert operations.
MongoDB Operations Manual crud Insertion