我有一個變量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]
你是指「測驗」字段中「」正確「字段的含義?請定義你想要的輸出,以便我可以幫助 –
請參閱:http://stackoverflow.com/questions/4969768/removing-the-array-element-in-mongodb-based-on-the-position-of-element 在MongoDB中這樣做並不是一種好方法,它已經成爲JIRA項目的一段時間了。 – dyouberg
@Himanshusharma感謝您的回覆,我的意思是數組中每個對象都被命名爲「正確」的字段。編輯:這種情況下的輸出將是一個數組,[0,2,0,2,2] –