在MongoDB中查找并替换子子数组元素 [英] Find and replace a sub sub array element in MongoDB

查看:46
本文介绍了在MongoDB中查找并替换子子数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发MeteorJS应用,我被困在更新子子数组元素上.

Hi I am developing MeteorJS app, I am stuck at updating a sub sub array element.

这是一个民意调查应用程序,并且我具有以下数据库结构:

It is a poll application and I have the following database structure:

在每个问题下都有选项,当用户单击某个选项的按钮时,我想将选项的投票数增加一,每个用户对每个问题应有一个投票权.

Under each question there are options and when a user clicks a button of an option, I want to increment that options votes by one and every user should have one vote right for each question.

从按钮中,我正在传递名称和QuestionId数据,以便找到增加投票的正确选项.我应该找到带有QuestionId的特定问题,然后找到带有选项下名称的特定数组.

From the button, I am passing name and questionId data in order to find the right option to increment vote. I should find the specific question with the questionId and then the specific array with the name under the Options.

我无法找到的地方.

请帮助,谢谢

馆藏名称:民意调查

每个投票具有以下结构:

Each Poll has the following structure:

{
    "_id" : "uJtBt8mM2pbTYfwND",
    "createdAt" : ISODate("2017-04-03T22:40:14.678Z"),
    "pollName" : "First Poll",
    "entryOwner" : "gdAHxDrxFuTvYiFt8",
    "question" : [ 
        {
            "name" : "Question number 1",
            "questionId" : "xgYQxGxpwBXaQpjXN",
            "options" : [ 
                {
                    "name" : "John",
                    "votes" : 0
                }, 
                {
                    "name" : "Adam",
                    "votes" : 0
                }, 
                {
                    "name" : "Robert",
                    "votes" : 0
                }
            ]
        }, 
        {
            "name" : "Question number 2",
            "questionId" : "zviwYHHsaATBdG6Jw",
            "options" : [ 
                {
                    "name" : "John",
                    "votes" : 0
                }, 
                {
                    "name" : "Adam",
                    "votes" : 0
                }, 
                {
                    "name" : "Robert",
                    "votes" : 0
                }
            ]
        }
    ],
}

推荐答案

您可以使用$ and在两个或多个表达式的数组上执行逻辑AND操作.

You can use $and which performs a logical AND operation on an array of two or more expressions.

{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }

这里的第一个表达式是使用questionId来获取问题.

The first expression here would be to get the question with questionId.

'question.questionId': "xgYQxGxpwBXaQpjXN"

第二个表达式,用于在options数组中指定具有匹配名称的对象.要在选项数组中找到具有名称的对象,可以使用$ elemMatch,它可以指定查询.

And second expression to specify the object with matching name in options array. To find the object with the name in options array, you can use $elemMatch which allows to specify queries.

{ <field>: { $elemMatch: { <query1>, <query2>, ... } } }

要获取名称为"John"的选项数组中的对象.

To get the object in options array having name as "John".

'question.options': {
    $elemMatch: {
        name: "John"
    }
}

最后,使用$ inc增加投票(此处为1).它将获得第一个匹配元素(带有$).

And finally, use $inc to increase the votes (here by 1). It will get the first matching element (with $).

'question.options.$.votes': 1

这是完整的代码:

db.Polls.update({
    $and: [{
            'question.questionId': "xgYQxGxpwBXaQpjXN"
        },
        {
            'question.options': {
                $elemMatch: {
                    name: "John"
                }
            }
        }
    ]
}, {
    $inc: {
        'question.options.$.votes': 1
    }
})

这篇关于在MongoDB中查找并替换子子数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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