在mongo中检索emedded对象的值

时间:2016-09-22 07:08:59

标签: mongodb

跟进问题

感谢@ 4J41为您提供解决方案。同样,我也想验证另一件事。

我有一个包含字符串数组的mongo文档,我需要将这个特定的字符串数组转换为包含键值对的对象数组。以下是我对它的不切实际的评价。

Mongo记录:  我在下面的初始问题中记录了相同的mongo记录。

当前查询:

templateAttributes.find({platform:"V1"}).map(function(c){
  //instantiate a new array
  var optionsArray = [];
for (var i=0;i< c['available']['Community']['attributes']['type']['values'].length; i++){
    optionsArray[i] = {};              // creates a new object
    optionsArray[i].label = c['available']['Community']['attributes']['type']['values'][i];
    optionsArray[i].value = c['available']['Community']['attributes']['type']['values'][i];
    }
    return optionsArray;
})[0];

结果:

[{label:"well-known", value:"well-known"},
{label:"simple", value:"simple"},
{label:"complex", value:"complex"}]

我的方法是否足够有效,或者有没有办法优化上述查询以获得相同的预期结果?

初步问题

我有一个类似下面的mongo文档:

{
    "_id" : ObjectId("57e3720836e36f63695a2ef2"),
    "platform" : "A1",
    "available" : {
        "Community" : {
            "attributes" : {
                "type" : {
                    "values" : [
                        "well-known",
                        "simple",
                        "complex"
                    ],
                    "defaultValue" : "well-known"
                },
[......]


}

我正在尝试查询数据库并仅检索defaultValue字段的值。

我试过了:

db.templateAttributes.find(
    { platform: "A1" },   
    { "available.Community.attributes.type.defaultValue": 1 }
)

以及

db.templateAttributes.findOne(
    { platform: "A1" },    
    { "available.Community.attributes.type.defaultValue": 1 }
)

但他们似乎都像下面一样检索整个对象:

{
    "_id" : ObjectId("57e3720836e36f63695a2ef2"),
    "available" : {
        "Community" : {
            "attributes" : {
                "type" : {
                    "defaultValue" : "well-known"
                }
            }
        }
    }
}

我能让它工作的唯一方法是使用find和map功能,但它似乎有点复杂。

有没有人有更简单的方法来获得这个结果?

db.templateAttributes.find(
    { platform: "A1" },    
    { "available.Community.attributes.type.defaultValue": 1 }
).map(function(c){
    return c['available']['Community']['attributes']['type']['defaultValue']
})[0]

输出

well-known

1 个答案:

答案 0 :(得分:1)

您可以尝试以下操作。

使用find:

db.templateAttributes.find({ platform: "A1" }, { "available.Community.attributes.type.defaultValue": 1 }).toArray()[0]['available']['Community']['attributes']['type']['defaultValue']

使用findOne:

db.templateAttributes.findOne({ platform: "A1" }, { "available.Community.attributes.type.defaultValue": 1 })['available']['Community']['attributes']['type']['defaultValue']

使用汇总:

db.templateAttributes.aggregate([
                                  {"$match":{platform:"A1"}}, 
                                  {"$project": {_id:0, default:"$available.Community.attributes.type.defaultValue"}}
                             ]).toArray()[0].default

<强>输出:

well-known

编辑:回答更新的问题:请在此处使用聚合。

db.templateAttributes.aggregate([
                                    {"$match":{platform:"A1"}}, {"$unwind": "$available.Community.attributes.type.values"}, 
                                    {$group: {"_id": null, "val":{"$push":{label:"$available.Community.attributes.type.values", 
                                                                            value:"$available.Community.attributes.type.values"}}}}
                                ]).toArray()[0].val

<强>输出:

[
    {
            "label" : "well-known",
            "value" : "well-known"
    },
    {
            "label" : "simple",
            "value" : "simple"
    },
    {
            "label" : "complex",
            "value" : "complex"
    }
]
相关问题