如何在node.js中使用mongoose中的findOne for循环,也许还有其他逻辑可以做同样的事情?
data = { _id: '598b4e9abe74280be0b0cb1b' }
{ _id: '598b4ec1be74280be0b0cb1c' }
{ _id: '598b4fa8be74280be0b0cb1d' }
{ _id: '5992feccdad6db0c603b260d' }
{ _id: '5995e4ce0b38ba123cf06654' };
notification = new Array();
for (var i = 0; i < data.length; i++) {
Message.findOne({ from: data[i]._id, to:socket.handshake.session.passport.user }, function(err, message){
if (err){
console.log('error');
}
if(!message.length){
console.log('no message!!');
}else{
notification.push(message);
}
});
}
答案 0 :(得分:0)
如果您可以将输入数据转换为数组,则可以执行以下操作:
var data = [ObjectId('598b4e9abe74280be0b0cb1b'),
ObjectId('598b4ec1be74280be0b0cb1c'),
ObjectId('598b4fa8be74280be0b0cb1d'),
ObjectId('5992feccdad6db0c603b260d'),
ObjectId('5995e4ce0b38ba123cf06654')];
Message.find({from: {$in: data}, to:socket.handshake.session.passport.user})