3

I know this had been asked a million times but I could not find my exact scenario, so here we go:

my data looks like this:

{
"_id" : ObjectId("55bf22a73d64bd3c495adf3d"),
"_data_type" : "questionnaire",
"title" : "Food quality",
"_namespace" : "questionnaire",
"description" : "tell us your opinion",
"created_time" : ISODate("2015-08-03T08:13:27.874Z"),
"questions" : {
    "sub_id_1" : {
        "o2" : "Bad",
        "o1" : "Good",
        "_namespace" : "questionnaire::question",
        "question" : "How do you rate our food quality"
    },
    "sub_id_2" : {
        "o2" : "Bad",
        "o1" : "Good",
        "_namespace" : "questionnaire::question",
        "question" : "How do you rate the colouring of our dishes "
    },
    "sub_id_3" : {
        "o2" : "I don't care!",
        "o1" : "It is greate",
        "_namespace" : "questionnaire::question",
        "question" : "How do you like our couches"
    }
},
"updated_time" : ISODate("2015-08-03T08:20:37.982Z")
}

I want to get all the questions that have "o2" equal "Bad".

I tried the following but no luck.

> db.questionnaire.find({"questions.$.o2": "Bad"})
> db.questionnaire.find({"questions.o2": "Bad"})
3
  • Can you show us the expected result? Commented Aug 9, 2015 at 19:03
  • It want to return all the documents containing a "questions" array that has at least one question with o2 = "Bad" Commented Aug 9, 2015 at 20:05
  • I don't think this is an option at the moment! Anyway to work around it?! Commented Aug 9, 2015 at 20:12

1 Answer 1

3

With your current documents structure you need to use the $where operator which does a JavaScript evaluation.

db.collection.find(function(){
    for (var key in this.questions) {     
        if (this.questions[key]["o2"] == "Bad")         
            return true; 
    }
})

But I suggest you change your document structure because generally you don't want to use $where. To do that you need to use the Bulk() Api.

var bulk = db.collection.initializeUnorderedBulkOp();
var questions = [];
var subdoc = {};
var count  = 0;

db.collection.find().forEach(function(doc) { 
    for (var key in doc.questions) {     
        subdoc["o2"] = doc.questions[key]["o2"];    
        subdoc["o1"] = doc.questions[key]["o1"];    
        subdoc["_namespace"] = doc.questions[key]["_namespace"]; 
        subdoc["question"] = doc.questions[key]["question"];   
        subdoc["id"] = key;     
        questions.push(subdoc); 
        subdoc = {};
    } 

    bulk.find({ "_id": doc._id }).updateOne({
        "$set": { "questions": questions }});
    count++; 
    if (count % 1000 == 0) {  
        // Execute per 1000 operations and re-init.   
        bulk.execute();    
        bulk = db.collection.initializeUnorderedBulkOp(); 
    } 
})

// Clean up queues
if ( count % 1000 != 0 ) { 
    bulk.execute(); 
}

Then you documents look like this:

> db.collection.find().pretty()
{
        "_id" : ObjectId("55bf22a73d64bd3c495adf3d"),
        "_data_type" : "questionnaire",
        "title" : "Food quality",
        "_namespace" : "questionnaire",
        "description" : "tell us your opinion",
        "created_time" : ISODate("2015-08-03T08:13:27.874Z"),
        "questions" : [
                {
                        "o2" : "Bad",
                        "o1" : "Good",
                        "_namespace" : "questionnaire::question",
                        "question" : "How do you rate our food quality",
                        "id" : "sub_id_1"
                },
                {
                        "o2" : "Bad",
                        "o1" : "Good",
                        "_namespace" : "questionnaire::question",
                        "question" : "How do you rate the colouring of our dishes ",
                        "id" : "sub_id_2"
                },
                {
                        "o2" : "I don't care!",
                        "o1" : "It is greate",
                        "_namespace" : "questionnaire::question",
                        "question" : "How do you like our couches",
                        "id" : "sub_id_3"
                }
        ],
        "updated_time" : ISODate("2015-08-03T08:20:37.982Z")
}

You query become simpler and can use indexes if any.

db.collection.find({ "questions.o2": "Bad" })
Sign up to request clarification or add additional context in comments.

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.