Javascript 如何在复杂的查询中使用mongodb和node js
我想在节点js中使用mongodb。 我可以插入、更新,。。。。在带有节点的mongo中。例如,我可以使用“查找”进行选择,但无法为查询2输入一个查询1的结果Javascript 如何在复杂的查询中使用mongodb和node js,javascript,node.js,mongodb,mongoose,node-modules,Javascript,Node.js,Mongodb,Mongoose,Node Modules,我想在节点js中使用mongodb。 我可以插入、更新,。。。。在带有节点的mongo中。例如,我可以使用“查找”进行选择,但无法为查询2输入一个查询1的结果 您可以首先从集合中获取与where查询匹配的所有记录,然后循环到这些记录,以仅更新具有c=4的结果记录: MyCollectionName.find({a:{$gt: 4}, b:{$gt: 3}}, function findResult(err, findResult) { if (err) { console.
您可以首先从集合中获取与
where
查询匹配的所有记录,然后循环到这些记录,以仅更新具有c=4
的结果记录:
MyCollectionName.find({a:{$gt: 4}, b:{$gt: 3}}, function findResult(err, findResult) {
if (err) {
console.log('Error on find');
return;
}
findResult.forEach(function (result) {
if(result.c === 4){
result.c = 5;
result.save(function saveResult(err, savedResult) {
if (err) {
console.log('Error on save');
return;
}
console.log('Updated...');
return;
});
}
});
});
其中,MyCollectionName
是要查询的集合的mongoose模型