如何将 findAndModify 与 Node.js MongoDB 驱动程序一起使用? [英] How to use findAndModify with a Node.js MongoDB driver?

查看:39
本文介绍了如何将 findAndModify 与 Node.js MongoDB 驱动程序一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码有什么问题?

What is wrong with the code below?

db.get().collection('bars').findAndModify({
    barID: req.body.button
},{
    $push: {
        usersfbID: req.body.profileUser[0].facebookID,
        usersDocID: req.body.profileUser[0]._id
    }
},{
    new: true
}, function(err, doc){
    if(err){
        throw err;
    }if(doc){
        console.log('Existing document updated successfully');
        console.log(doc);
    }
});

我总是遇到同样的错误,

I always get the same error,

MongoError: 异常: nextSafe(): { $err: "无法规范化查询:BadValue 错误排序规范",代码:17287 }

MongoError: exception: nextSafe(): { $err: "Can't canonicalize query: BadValue bad sort specification", code: 17287 }

我做错了什么?我是否滥用了 $push 运算符?我是否需要在某些键值对周围提供引号?这个 findAndModify 方法让我发疯.我完全不知道我做错了什么.

What am I doing wrong? Am I misusing the $push operator? Do I need to provide quotes around some of the key value pairs? This findAndModify method is driving me crazy. I have absolutely no idea what I am doing wrong.

注意:我已经解决了关于 findAndModify 的其他问题,但这些解决方案都不适用于我.我确定这是一个关于函数语法的小错误.

Note: I have been through the other questions on SO regarding findAndModify, but none of those solutions is working for me. I'm sure its a small error regarding the syntax for the function.

推荐答案

findAndModify 方法具有以下签名

findAndModify(query, sort, doc, options, callback)

哪里