1

I have a variable var correctAnswers;

In my MongoDB I have the following document (below). I am trying to write a query that takes all of the "correct" fields from the "quiz" field and put them into their own array, so I can set that array equal to 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
        }]"

How should I go about that, or can someone point me in the right direction? I have tried projections, but should I do an aggregation? Thank you for any help.

Edit for clarity: the output I am looking for in this example is an array, [0,2,0,2,2]

Jake 1986
  • 582
  • 1
  • 6
  • 25
  • what you mean by " "correct" fields from the "quiz" field" ? please define what output you want so that i can help – Himanshu sharma Sep 13 '16 at 16:29
  • See here: http://stackoverflow.com/questions/4969768/removing-the-array-element-in-mongodb-based-on-the-position-of-element There isn't a great way of doing this in MongoDB yet and it's been a JIRA item for some time. – dyouberg Sep 13 '16 at 16:30
  • @Himanshusharma thank you for your reply, I mean the field that is named "correct" in each object in the array. Edit: the output in this case would be an array, [0,2,0,2,2] – Jake 1986 Sep 13 '16 at 16:36
  • Provide some code in node.js as you trying to do. Do you think to use Mongoose? – BrTkCa Sep 13 '16 at 16:41
  • It seems like you're trying to get an array so you can produce an average? I'm after the same thing and also haven't found a good solution yet. – Nick Schroeder Sep 14 '16 at 01:44

4 Answers4

0

you can get this result [{correct:0},{correct:2},{correct:0},{correct:2}] but [0,2,0,2,2] type of result is not possible unless we use distinct

db.quiz.aggregate( // Initial document match (uses index, if a suitable one is available) { $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"
}} )
Himanshu sharma
  • 7,487
  • 4
  • 42
  • 75
0
db.users.find( { }, { "quiz.correct": 1,"_id":0 } ) 

// above query will return following output :

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

Process this output as per requirement in the node js.

Vora Ankit
  • 682
  • 5
  • 8
0

One way to achieve this through aggregation

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

Above query will give you the following output

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

Then simply process this output as per your need.

RootHacker
  • 1,109
  • 8
  • 12
0

Try this:

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

It will give you the expected output.

dds
  • 2,335
  • 1
  • 31
  • 45
Vaibhav Patil
  • 2,603
  • 4
  • 14
  • 22