如何将MongoDB查询中的嵌入式文档拉入数组?

时间:2016-09-13 16:24:41

标签: node.js mongodb express

我有一个变量var correctAnswers;

在我的MongoDB中,我有以下文档(如下)。我正在尝试编写一个查询,它从“测验”字段中获取所有“正确”字段并将它们放入自己的数组中,因此我可以将该数组设置为var correctAnswers;

"title" : "Economics questions"
"quiz": "[{
          "question": "Which of these involves the analysis of of a business's financial statements, often used in stock valuation?",
          "choices": ["Fundamental analysis", "Technical analysis"],
          "correct": 0
        }, {
          "question": "What was the name of the bond purchasing program started by the U.S. Federal Reserve in response to the 2008 financial crisis?",
          "choices": ["Stimulus Package", "Mercantilism", "Quantitative Easing"],
          "correct": 2
        }, {
          "question": "Which term describes a debt security issued by a government, company, or other entity?",
          "choices": ["Bond", "Stock", "Mutual fund"],
          "correct": 0
        }, {
          "question": "Which of these companies has the largest market capitalization (as of October 2015)?",
          "choices": ["Microsoft", "General Electric", "Apple", "Bank of America"],
          "correct": 2
        }, {
          "question": "Which of these is a measure of the size of an economy?",
          "choices": ["Unemployment rate", "Purchasing power index", "Gross Domestic Product"],
          "correct": 2
        }]"

我应该怎么做,或者有人指出我正确的方向?我尝试过预测,但是我应该进行聚合吗?谢谢你的帮助。

为了清晰起见编辑:我在这个例子中寻找的输出是一个数组,[0,2,0,2,2]

4 个答案:

答案 0 :(得分:0)

你可以得到这个结果 [{correct:0},{correct:2},{correct:0},{correct:2}]但是[0,2,0,2,2]类型的结果是不可能的,除非我们使用distinct

  

db.quiz.aggregate(       //初始文档匹配(使用索引,如果有合适的文档)       {$ match:{           “标题”:“经济学问题”       }},

// Convert embedded array into stream of documents
{ $unwind: '$quiz' },

    },

// Note: Could add a `$group` by _id here if multiple matches are expected

// Final projection: exclude fields with 0, include fields with 1
{ $project: {
    _id: 0,
    score: "$quiz.correct"
}} )

答案 1 :(得分:0)

db.users.find( { }, { "quiz.correct": 1,"_id":0 } ) 

//上面的查询将返回以下输出:

{
        "quiz" : [
                {
                        "correct" : 0
                },
                {
                        "correct" : 2
                },
                {
                        "correct" : 0
                },
                {
                        "correct" : 2
                },
                {
                        "correct" : 2
                }
        ]
}

根据节点js中的要求处理此输出。

答案 2 :(得分:0)

通过聚合实现这一目标的一种方法

db.collectionName.aggregate([
     // use index key in match pipeline,just for e.g using title here
    { $match: { "title" : "Economics questions" }},
    { $unwind: "$quiz" },
    { $group: {
          _id:null,
          quiz: { $push:  "$quiz.correct" }
        } 
    },
    //this is not required, use projection only if you want to exclude/include fields 
    { 
        $project: {_id: 0, quiz: 1}
    } 
])

以上查询将为您提供以下输出

{
    "quiz" : [ 0, 2, 0, 2, 2 ]
}

然后根据需要简单处理此输出。

答案 3 :(得分:0)

试试这个:

db.getCollection('quize').aggregate([
{$match:{_id: id }},
{$unwind:'$quiz'}, 
{$group:{
_id:null, 
score: {$push:"$quiz.correct"} 
}}
])

它会给你预期的输出。