mongodb嵌套对象数组查询

时间:2015-08-09 18:46:19

标签: javascript mongodb mongodb-query

我知道这已被问了一百万次,但我找不到我的具体情况,所以我们走了:

我的数据如下:

{
"_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")
}

我想得到所有“o2”等于“坏”的问题。

我尝试了以下但没有运气。

> db.questionnaire.find({"questions.$.o2": "Bad"})
> db.questionnaire.find({"questions.o2": "Bad"})

1 个答案:

答案 0 :(得分:3)

使用当前的文档结构,您需要使用执行JavaScript评估的$where运算符。

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

但我建议您更改文档结构,因为通常you don't want to use $where。为此,您需要使用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(); 
}

然后你的文件看起来像这样:

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

您的查询变得更简单,并且可以使用索引(如果有的话)。

db.collection.find({ "questions.o2": "Bad" })