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]

5
  • what you mean by " "correct" fields from the "quiz" field" ? please define what output you want so that i can help Commented Sep 13, 2016 at 16:29
  • See here: stackoverflow.com/questions/4969768/… There isn't a great way of doing this in MongoDB yet and it's been a JIRA item for some time. Commented Sep 13, 2016 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] Commented Sep 13, 2016 at 16:36
  • Provide some code in node.js as you trying to do. Do you think to use Mongoose? Commented Sep 13, 2016 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. Commented Sep 14, 2016 at 1:44

4 Answers 4

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"
}} )
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.