Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MongoDB

Viết câu lệnh MongoDB insert nhiều tài liệu cùng lúc

Để chèn nhiều tài liệu cùng lúc, sử dụng phương thức insert() với mảng các đối tượng.

Chèn 3 tài liệu một lần:

> db.demo689.insert([
...     {ClientName:"Chris","ClientAge":34,"ClientCountryName":"US"},
...     {ClientName:"David","ClientAge":28,"ClientCountryName":"UK"},
...     {ClientName:"Bob","ClientAge":39,"ClientCountryName":"AUS"},
... ]);
BulkWriteResult({
    "writeErrors" : [ ],
    "writeConcernErrors" : [ ],
    "nInserted" : 3,
    "nUpserted" : 0,
    "nMatched" : 0,
    "nModified" : 0,
    "nRemoved" : 0,
    "upserted" : [ ]
})

Hiển thị tất cả:

> db.demo689.find();

Kết quả (tự động tạo _id):

{ "_id" : ObjectId("5ea580dfa7e81adc6a0b3967"), "ClientName" : "Chris", "ClientAge" : 34, "ClientCountryName" : "US" }
{ "_id" : ObjectId("5ea580dfa7e81adc6a0b3968"), "ClientName" : "David", "ClientAge" : 28, "ClientCountryName" : "UK" }
{ "_id" : ObjectId("5ea580dfa7e81adc6a0b3969"), "ClientName" : "Bob", "ClientAge" : 39, "ClientCountryName" : "AUS" }