1. The document's data storage format is Bson, similar to JSON. When MongoDB inserts data, it verifies that there is a "_id" field in the data if it is not automatically generated.
There are two ways to insert and save a shell operation. When inserting a piece of data with a value of "_id" and now has the same value in the collection, inserting it with insert is not inserted, and when you use Save, the data is updated.
1>Db.student.drop ()2 true3> Db.student.insert ({"_id": 1, "name": "Zhangsan", "Age": 28})4Writeresult ({"ninserted": 1 })5>Db.student.find ()6{"_id": 1, "name": "Zhangsan", "Age": 28 }7> Db.student.insert ({"_id": 1, "name": "Zhangsan", "Age": 27})8 Writeresult ({9"Ninserted": 0,Ten"Writeerror" : { One"Code": 11000, A"ErrMsg": "E11000 duplicate key error collection:zyhdb.student index: _id_ dup key: {: 1.0}" - } - }) the>Db.student.find () -{"_id": 1, "name": "Zhangsan", "Age": 28 } -> Db.student.save ({"_id": 1, "name": "Zhangsan", "Age": 27}) -Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1 }) +>Db.student.find () -{"_id": 1, "name": "Zhangsan", "Age": 27}
2, BULK INSERT, online documents are said not MongoDB does not support BULK INSERT, now tried can, should be the current version support bulk INSERT.
1> Db.student.insert ([{"_id": 2, "name": "Lisi"},{"_id": 3, "name": "Wangwu"}, {"_id": 4, "name": "Zhaoliu", "Age": 28}])2 Bulkwriteresult ({3"Writeerrors" : [ ],4"Writeconcernerrors" : [ ],5"Ninserted": 3,6"Nupserted": 0,7"Nmatched": 0,8"Nmodified": 0,9"Nremoved": 0,Ten"Upserted" : [ ] One }) A>Db.student.find () -{"_id": 1, "name": "Zhangsan", "Age": 27 } -{"_id": 2, "name": "Lisi" } the{"_id": 3, "name": "Wangwu" } -{"_id": 4, "name": "Zhaoliu", "Age": 28}
3. Loop Insert:
1> for(vari=0; i<10; i++) {Db.fortest.insert ({num:i})}2Writeresult ({"ninserted": 1 })3>Db.fortest.find ()4{"_id": ObjectId ("57469e80142cea1d9aeabab5"), "num": 0 }5{"_id": ObjectId ("57469e80142cea1d9aeabab6"), "num": 1 }6{"_id": ObjectId ("57469e80142cea1d9aeabab7"), "num": 2 }7{"_id": ObjectId ("57469e80142cea1d9aeabab8"), "num": 3 }8{"_id": ObjectId ("57469e80142cea1d9aeabab9"), "num": 4 }9{"_id": ObjectId ("57469e80142cea1d9aeababa"), "num": 5 }Ten{"_id": ObjectId ("57469e80142cea1d9aeababb"), "num": 6 } One{"_id": ObjectId ("57469e80142cea1d9aeababc"), "num": 7 } A{"_id": ObjectId ("57469e80142cea1d9aeababd"), "num": 8 } -{"_id": ObjectId ("57469e80142cea1d9aeababe"), "num": 9}
MongoDB Quick Start Learning Note 3 MongoDB document insertion operation