猫鼬-增加嵌套数组中的字段值 [英] Mongoose - increment a field value in nested array

查看:60
本文介绍了猫鼬-增加嵌套数组中的字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Mongoose和MongoDB的新手,正在尝试实现一种方案,其中我必须更新嵌套数组中的字段值.

I'm new to Mongoose and MongoDB and am trying to implement a scenario where I have to update a field value residing in a nested array.

例如,我的数据库条目如下:

For example, my DB entry is as follows:

{
    "_id" : ObjectId("581999ecf97860b77c1625f6"),      // this is userID
    "username" : "test",
    "__v" : 0,
    "polls" : [
            {
                    "url" : "xxxx",
                    "question" : "city or united",
                    "_id" : ObjectId("581999ec3ba2b15ad7fbcd50"),    //this is questionID
                    "option" : [
                            {
                                    "votes" : 0,
                                    "value" : "city",
                                    "_id" : ObjectId("581999ec3ba2b15ad7fbcd52")    //this is optionID
                            },
                            {
                                    "votes" : 0,
                                    "value" : "united",
                                    "_id" : ObjectId("581999ec3ba2b15ad7fbcd51")    //this is optionID
                            }
                    ]
            },
            {
                    "url" : "yyyy",
                    "question" : "chelsea or arsenal",
                    "_id" : ObjectId("58199d0ef11835685d3f41b7"),
                    "option" : [
                            {
                                    "votes" : 0,
                                    "value" : "chelsea",
                                    "_id" : ObjectId("58199d0ef11835685d3f41b9")
                            },
                            {
                                    "votes" : 0,
                                    "value" : "arsenal",
                                    "_id" : ObjectId("58199d0ef11835685d3f41b8")
                            }
                    ]
            }
    ]
}

无论何时从前端选择某个特定选项,我都需要增加其投票数.

I need to increment the votes of a particular option whenever it is selected from the front-end.

下面是到目前为止我尝试实现的目标,但是没有成功.

Below is what I've tried to implement so far but without success.

PollsSchema.methods.submitVote = function(id, callback){
var increment = { $inc: { 'polls.option.$.votes': 1 } };
var query = { 
    '_id': id.userID,
    'polls._id': id.questionID, 
    'polls.option._id': id.optionID
};

return this.model('Poll').update(query, increment, callback);
};

以下是我的架构:

var PollsSchema = new Schema({
username: String,
polls: [
    {
        question: String,
        option: [
            {
                value: String,
                votes: Number
            }
        ],
        url: String
    }
]
});

在这里我做错了什么吗?

Is there something that I'm doing incorrectly over here?

感谢您的时间和建议.

推荐答案

您遇到了Mongo db限制,该限制不支持嵌套数组占位符替换.位置运算符只能用于一个级别.因此,如果您要更新民意调查下的民意调查.$.question之类的问题,它将正常工作.另外,mongo db不支持多个占位符,例如polls.$.option.$.votes.

You've the hit the Mongo db limitation where it doesn't support the nested array placeholder replacement. The positional operator can only be used for one level. So if you were to update the question like polls.$.question under polls, it will work fine. Also, mongo db doesnt have support for multiple placeholder something like polls.$.option.$.votes.

如评论中所指出的,您可以查看该jira缺陷.现在要解决.

As pointed in the comment you can view that jira defect. Now coming to a workaround.

您可以展平您的选择,以便我们可以将其包含在民意调查中.下面是这样的.只要您对问题和选项具有唯一的ID,就应该始终可以解析为唯一的记录.

You can flatten your options so we can have it inside the polls. Something like this below. As long as you have unique id for question and option you should always can resolve to unique record.

{
    "_id": ObjectId("581999ecf97860b77c1625f6"), // this is userID
    "username": "test",
    "__v": 0,
    "polls": [{
        "url": "xxxx",
        "question": "city or united",
        "_id": ObjectId("581999ec3ba2b15ad7fbcd50"), //this is questionID
        "votes": 0,
        "value": "city",
        "option_id": ObjectId("581999ec3ba2b15ad7fbcd52") //this is optionID
    }, {
        "url": "xxxx",
        "question": "city or united",
        "_id": ObjectId("581999ec3ba2b15ad7fbcd50"), //this is questionID
        "votes": 0,
        "value": "united",
        "option_id": ObjectId("581999ec3ba2b15ad7fbcd51") //this is optionID
    }]
}

现在您可以使用类似的方法来增加选票.

Now you can use something like this to increment the votes.

PollsSchema.methods.submitVote = function(id, callback) {
    var increment = {
        $inc: {
            'polls.$.votes': 1
        }
    };
    var query = {
        '_id': id.userID,
        'polls._id': id.questionID,
        'polls.option_id': id.optionID
    };

    return this.model('Poll').update(query, increment, callback);
};

其他选择是使用聚合管道并找到项目,然后更新返回的对象.

Other option is to use aggregate pipeline and find the item and then update that returned object.

这篇关于猫鼬-增加嵌套数组中的字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆