updateMany()之后如何获取所有更新文档的值?

use*_*287 5 mongodb node.js socket.io

期望的行为

更新多个文档中的属性值后,访问每个已更新文档的已更新属性的值。

(对于上下文,我增加多个用户的通知计数,然后将每个更新的值发送回相应的用户(如果他们已登录socket.io

我尝试过的

let collection = mongo_client.db("users").collection("users"); 
let filter = { "user_email": { $in: users_to_notify } };
let update = { $inc: { "new_notifications": 1 } };
let result = await collection.updateMany(filter, update);

// get the `new_notifications` value for all updated documents here

Run Code Online (Sandbox Code Playgroud)

似乎没有适用于该方法的returnOriginal类型。 optionupdateMany()

https://docs.mongodb.com/manual/reference/method/db.collection.updateMany

http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#updateMany

这是可以理解的,因为returnOriginal似乎只有在更新一个文档时才有意义,例如在findOneAndUpdate()方法的选项中。

问题

如果上述所有假设均成立,那么在方法完成 new_notifications后获取所有更新文档的值的最佳方法是什么?updateMany()

是否只是进行另一个数据库调用来获取更新的值?


例如 - 这有效:

let collection = mongo_client.db("users").collection("users"); 
let filter = { "user_email": { $in: users_to_notify } };
let update = { $inc: { "new_notifications": 1 } };
await collection.updateMany(filter, update);

// make another database call to get updated values  
var options = { projection: { user_email: 1, username: 1, new_notifications: 1 } };
let docs = collection.find(filter, options).toArray(); 

/*expected result: 
[{
    "user_email": "user_1@somedomain.com",
    "username": "user_1",
    "new_notifications": 17

},
{
    "user_email": "user_2@somedomain.com",
    "username": "user_2",
    "new_notifications": 5

}]*/  

// create an object where each doc's user_email is a key with the value of new_notifications  
var new_notifications_object = {};

// iterate over docs and add each property and value  
for (let obj of docs) {
        new_notifications_object[obj.user_email] = obj.new_notifications; 
}

/*expected result: 
{
    "user_1@somedomain.com": 17,
    "user_2@somedomain.com": 3  
}*/  

// iterare over logged_in_users
for (let key of Object.keys(logged_in_users)) {

    // get logged in user's email and socket id 
    let user_email = logged_in_users[key].user_email;
    let socket_id = key;

        // if the logged in user's email is in the users_to_notify array
        if (users_to_notify.indexOf(user_email) !== -1) {
            // emit to the socket's personal room  
            let notifications_count = new_notifications_object[user_email]; 
            io.to(socket_id).emit('new_notification', { "notifications_count": notifications_count });
        }

}
Run Code Online (Sandbox Code Playgroud)

小智 0

updateMany() 不返回更新的文档,而是返回一些计数。您只需运行另一个查询即可获取这些更新。建议您首先执行 find({}),然后在找到的项目的循环中执行 findByIdAndUpdate() 。

let updatedRecord = [];
let result = await Model.find(query);
if (result.length > 0) {
  result.some(async e => {
    updatedRecord.push(await Model.findByIdAndUpdate(e._id, updateObj, { new: true }));
  });
}
return updatedRecord;
Run Code Online (Sandbox Code Playgroud)