Mongodb返回旧集合 [英] Mongodb return old collection
问题描述
router.post('/orders/finish', function(req, res, next) {
var order_id = req.body.order_id;
var user_id = req.body.user_id;
var table_id = '';
var result = [];
mongo.connect(url, function(err, db) {
assert.equal(null, err);
db.collection('tables').update({id: table_id, status: true}, {$set: {status: false}}, function(err, result) {
assert.equal(null, err);
});
var cursorTables = db.collection('tables').find({status: false});
cursorTables.forEach(function(doc, err) {
assert.equal(null, err);
result.push(doc);
}, function() {
db.close();
res.send(JSON.stringify(result));
});
});
我正在更新表集合并尝试得到它们,但是我没有更新而收集旧的集合,但是在下一个请求中它已经改变了。
I'm updating table collection and try to get them, but I get old collection without updating. However in the next request its changed.
推荐答案
当您进行.find()调用时,您的收藏尚未更新。
When you make your .find() call, your collection isn't done updating yet.
您可以选择在.update()调用的回调中调用.find(),或者您也可以根据您的版本使用promises或async / await。
You can choose to call .find() in the callback of your .update() call, or you could also use promises or async/await depending on your version.
另一个解决方案是将 findAndModify 与新的选项:
Another solution would be to use findAndModify with the new option:
可选。当为true时,返回修改后的文档而不是原始文件。 findAndModify()方法忽略了删除操作的新选项。默认值为false。
Optional. When true, returns the modified document rather than the original. The findAndModify() method ignores the new option for remove operations. The default is false.
这篇关于Mongodb返回旧集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!