【发布时间】:2016-02-26 05:40:39
【问题描述】:
我想要完成的事情: 我查询了一个包含大量文档的集合(对话)。 对于集合中的每个文档/对话,我想查询另一个集合(用户),以查看是否存在与该对话中的 ID 属性匹配的现有用户记录。所以基本上我想看看是否存在附加到对话的用户的用户记录。
Users = { uid:someNumber, 一堆其他属性};
我知道这是 node.js 的异步特性的问题。我一直在尝试使用 async.js 通过回调来解决这个问题。但我想我可能有错,或者没有正确使用它。
问题是对话数组中的每个对话项目都在查询一个项目,但是,由于“保存”尚未完成,“查找”查询永远不会看到有记录已经插入。这是我的代码。也许我在做一些明显错误的事情?所以本质上,检查对话记录,如果用户记录与对话记录上的用户重合,则不做任何事情,如果用户记录不存在,则创建记录。
Conversations.find().limit(1000).exec(function (err, data) {
//data is an array of conversations, i want to loop through each conversation and compare one of the attribute with an attribute on the Users table
async.each(data, function(item, callback1){
//item is a single conversation, on this item there is a participants object that holds two user objects(name, id, type)
async.each(item.participants, function(user, callback2){
//this is where i do my query to see if a user exists
Users.find({uid:user.participantId}).exec(function (err, results){
//if the user doesn't exist then create a user record
if(results.length == 0){
var user = new Users();
user.name =user.participantName;
user.uid = user.participantId;
user.type = user.participantType;
user.save(function(err, result){
console.log(result);
//after it has saved, callback2() so that the second item in the array will query against the Users table
callback2();
})
}
else{
callback2()
})
})
//first item in the conversations array is completed, callback1(), second item should now start
callback1();
});
})
【问题讨论】:
-
我只想改变你处理问题的方式。为什么不循环对话中的每个项目(简单的 forEach)并收集所有参与者,然后获取所有唯一(无异步,可以是 _.unique)用户。现在您需要检查它们是否存在(异步),如果不存在,则保存(异步)。另一种方式:只缓存新创建的用户(map: user by Id)并在调用Users.find之前检查缓存。
标签: node.js mongodb mongoose mongodb-query