आप प्रत्येक इनपुट को बल्क राइट के साथ उपयोग के लिए आकार देने के लिए Array.map का उपयोग कर सकते हैं।
ऐसा लगता है कि आप जिस व्यवहार का वर्णन कर रहे हैं वह email . का उपयोग कर रहा है प्रत्येक दस्तावेज़ की पहचान करने के लिए फ़ील्ड।
आपने यह नहीं बताया कि दस्तावेज़ों में अन्य क्षेत्रों के साथ क्या होना चाहिए। यदि आप अन्य क्षेत्रों को अकेला छोड़ना चाहते हैं, तो $set . का उपयोग करें आने वाले डेटा में उल्लिखित किसी भी फ़ील्ड को हटाने के लिए, $replace . का उपयोग करें ।
शेल में ऐसा दिखता है:
PRIMARY> db.users.find()
{ "_id" : 1, "email" : "example@sqldat.com", "name" : "one" }
{ "_id" : 2, "email" : "example@sqldat.com", "name" : "two" }
{ "_id" : 3, "email" : "example@sqldat.com", "name" : "three" }
PRIMARY> let users = [
{ email: "example@sqldat.com", name: "oneeee" },
{ email: "example@sqldat.com", name: "twoooo" },
{ email: "example@sqldat.com", name: "three" },
{ email: "example@sqldat.com", name: "four" }
]
PRIMARY> let bulkUpdate = db.users.initializeUnorderedBulkOp()
PRIMARY> users.forEach(u => bulkUpdate.find({email:u.email}).upsert().update({$set:u}))
PRIMARY> bulkUpdate.execute()
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 0,
"nUpserted" : 1,
"nMatched" : 3,
"nModified" : 2,
"nRemoved" : 0,
"upserted" : [
{
"index" : 3,
"_id" : ObjectId("5f5e79ff28ee536df4c4a88e")
}
]
})
PRIMARY> db.users.find()
{ "_id" : 1, "email" : "example@sqldat.com", "name" : "oneeee" }
{ "_id" : 2, "email" : "example@sqldat.com", "name" : "twoooo" }
{ "_id" : 3, "email" : "example@sqldat.com", "name" : "three" }
{ "_id" : ObjectId("5f5e79ff28ee536df4c4a88e"), "email" : "example@sqldat.com", "name" : "four" }
Model.bulkWrite() पर एक नज़र डालें नेवला में इसे कैसे करें के उदाहरण के लिए।