2016-09-13 156 views
1

我有一個變量var correctAnswers;如何將嵌入式文檔從MongoDB查詢拉入數組?

在我的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]

+0

你是指「測驗」字段中「」正確「字段的含義?請定義你想要的輸出,以便我可以幫助 –

+0

請參閱:http://stackoverflow.com/questions/4969768/removing-the-array-element-in-mongodb-based-on-the-position-of-element 在MongoDB中這樣做並不是一種好方法,它已經成爲JIRA項目的一段時間了。 – dyouberg

+0

@Himanshusharma感謝您的回覆,我的意思是數組中每個對象都被命名爲「正確」的字段。編輯:這種情況下的輸出將是一個數組,[0,2,0,2,2] –

回答

0

你可以得到這個結果 [{正確的:0},{正確:2},{正確:0},{正確:2}],但[0,2,0,2,2]類型的結果是不可能的,除非我們使用不同的

db.quiz.aggregate ( //初始文件匹配(使用索引,如果合適的一個可用) {$ match:{ 「title」:「Economics questions」 }},

// 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" 
}}) 
0
db.users.find({ }, { "quiz.correct": 1,"_id":0 }) 

//上面的查詢將返回以下輸出:

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

流程該輸出按規定在節點JS。

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 ] 
} 

然後簡單地處理這個輸出根據自己的需要。

0

試試這個:

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

它會給你預期的輸出。