Mongoose updateMany with different values by unique id like email without loop

房东的猫 提交于 2021-02-10 15:43:18

问题


Mongoose updateMany with different values by unique id like email
Old BD Data:

usersCollection = [  
    { _id: 1, email: "one@gmail.com", name: "one" },  
    { _id: 2, email: "two@gmail.com", name: "two" },  
    { _id: 3, email: "three@gmail.com", name: "three" }  
];

Coming Data:

users = [  
    { email: "one@gmail.com", name: "oneeee" },  
    { email: "two@gmail.com", name: "twoooo" },  
    { email: "three@gmail.com", name: "three" },  
    { email: "three@gmail.com", name: "four" }  
];

What I'm expecting to to have after insert
New DB Data:

users = [  
    { _id: 1, email: "one@gmail.com", name: "oneeee" },  
    { _id: 2, email: "two@gmail.com", name: "twoooo" },  
    { _id: 3, email: "three@gmail.com", name: "three" },  
    { _id: 4, email: "three@gmail.com", name: "four" },  
]

I'm Thinking like:

const newUsers = [  
    { email: "one@gmail.com", name: "oneeee" },  
    { email: "two@gmail.com", name: "twoooo" },  
    { email: "three@gmail.com", name: "three" },  
    { email: "three@gmail.com", name: "four" },  
];
const someQuery = {};
User.updateMany(someQuery, newUsers, {upsert: true}, (userErr, userDoc) => {  
    return userDoc;  
});

回答1:


You could use Array.map to shape each input for use with a bulk write.

The behavior you are describing seems to be using the email field to identify each document.

You didn't indicate what should happen with other fields in the documents. If you want to leave other fields alone, use $set to remove any fields not mentioned in the incoming data, use $replace.

In the shell this looks like:

PRIMARY> db.users.find()
{ "_id" : 1, "email" : "one@gmail.com", "name" : "one" }
{ "_id" : 2, "email" : "two@gmail.com", "name" : "two" }
{ "_id" : 3, "email" : "three@gmail.com", "name" : "three" }

PRIMARY> let users = [     
  { email: "one@gmail.com", name: "oneeee" },       
  { email: "two@gmail.com", name: "twoooo" },       
  { email: "three@gmail.com", name: "three" },       
  { email: "four@gmail.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" : "one@gmail.com", "name" : "oneeee" }
{ "_id" : 2, "email" : "two@gmail.com", "name" : "twoooo" }
{ "_id" : 3, "email" : "three@gmail.com", "name" : "three" }
{ "_id" : ObjectId("5f5e79ff28ee536df4c4a88e"), "email" : "four@gmail.com", "name" : "four" }

Take a look at Model.bulkWrite() for an example of how to do this in mongoose.



来源:https://stackoverflow.com/questions/63869407/mongoose-updatemany-with-different-values-by-unique-id-like-email-without-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!