MongoDB quick start notes of iii MongoDB insert document operation

  • 2020-05-30 21:15:44
  • OfStack

MongoDB is a database based on distributed file storage. Written by C++ language. Designed to provide scalable, high-performance data storage solutions for WEB applications.

MongoDB is a product between relational database and non-relational database. It is the most functional and relational database like non-relational database.

This article introduces you to MongoDB's method of inserting documents. Take a look at it

1. The data storage format of the document is BSON, similar to JSON. When MongoDB inserts data, it checks whether there is "_id" in the data. If not, it is generated automatically.

There are two methods for shell operation: insert and save. When a piece of data is inserted with the value "_id" and the same value is already in the collection, it cannot be inserted with insert. When save is used, the data will be updated.


> db.student.drop()
true
> db.student.insert({"_id": 1, "name":"zhangsan", "age": 28})
WriteResult({ "nInserted" : 1 })
> db.student.find()
{ "_id" : 1, "name" : "zhangsan", "age" : 28 }
> db.student.insert({"_id": 1, "name":"zhangsan", "age": 27})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: zyhdb.student index: _id_ dup key: { : 1.0 }"
}
})
> 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. Batch insert. The documents on the Internet all say that MongoDB does not support batch insert.


> db.student.insert([{"_id": 2, "name": "lisi"},{"_id": 3, "name": "wangwu"}, {"_id": 4, "name": "zhaoliu", "age": 28}])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
> db.student.find()
{ "_id" : 1, "name" : "zhangsan", "age" : 27 }
{ "_id" : , "name" : "lisi" }
{ "_id" : , "name" : "wangwu" }
{ "_id" : 4, "name" : "zhaoliu", "age" : 28 } 

3. Cyclic insertion:


> for(var i=; i<; i++){db.fortest.insert({num: i})}
WriteResult({ "nInserted" : })
> db.fortest.find()
{ "_id" : ObjectId("eceadaeabab"), "num" : 0}
{ "_id" : ObjectId("eceadaeabab"), "num" : 1}
{ "_id" : ObjectId("eceadaeabab"), "num" : 2}
{ "_id" : ObjectId("eceadaeabab"), "num" : 3}
{ "_id" : ObjectId("eceadaeabab"), "num" : 4}
{ "_id" : ObjectId("eceadaeababa"), "num" : 5}
{ "_id" : ObjectId("eceadaeababb"), "num" : 6}
{ "_id" : ObjectId("eceadaeababc"), "num" : 7}
{ "_id" : ObjectId("eceadaeababd"), "num" : 8}
{ "_id" : ObjectId("eceadaeababe"), "num" : 9}

The above is the site for you to introduce MongoDB quick introduction notes (3) MongoDB insert document operation knowledge, hope to help you, more exciting content, please pay attention to this site website!


Related articles: