Mongoose updateMany 具有不同值的唯一 ID,例如没有循环的电子邮件 [英] Mongoose updateMany with different values by unique id like email without loop

查看:76
本文介绍了Mongoose updateMany 具有不同值的唯一 ID,例如没有循环的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mongoose updateMany 具有不同的值,如电子邮件的唯一 ID
旧BD数据:

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" }  
];

即将到来的数据:

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" },  
]

我在想:

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;  
});

推荐答案

您可以使用 Array.map 对每个输入进行整形以用于批量写入.

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

您所描述的行为似乎是使用 email 字段来标识每个文档.

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

您没有说明文档中的其他字段应该发生什么.如果您想保留其他字段,请使用 $set 删除传入数据中未提及的任何字段,请使用 $replace.

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" }

查看 Model.bulkWrite() 的示例如何在猫鼬中做到这一点.

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

这篇关于Mongoose updateMany 具有不同值的唯一 ID,例如没有循环的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆