如何附加到mongo中的对象中的数组

时间:2015-09-04 10:51:46

标签: node.js mongodb mongoose

例如,我有以下架构:

{
    title:String,
    content:String,
    votes:{
        up:[ObjectId],
        down:[ObjectId]
    }
}

我希望追加up数组。如何使用Mongoose中的$ addToSet追加它?

我试过这些:

Article.findByIdAndUpdate(
    req.params.id, 
    {$addToSet:{votes:{up:req.user.id}}}, 
    null,
    function(asdf){
        console.log(asdf);
});

这是我收到的错误消息:

[MongoError: exception: Cannot apply $addToSet to a non-array field. Field named 'votes' has a non-array type Object in the document _id: ObjectId('55e975801026f4e02a17e77b')]

Article.findByIdAndUpdate(
    req.params.id, 
    {$addToSet:{votes.up:req.user.id}}, 
    null,
    function(asdf){
        console.log(asdf);
});

对于这个:

Article.findByIdAndUpdate(req.params.id, {$addToSet:{votes.up:ObjectID(req.u
                                                            ^
SyntaxError: Unexpected token .

0 个答案:

没有答案
相关问题