Mongodb 在mongo中检索eMeded对象的值 后续问题

Mongodb 在mongo中检索eMeded对象的值 后续问题,mongodb,Mongodb,感谢@4J41为您提供了解决方案。同样,我还想验证另一件事 我有一个包含字符串数组的mongo文档,我需要将这个字符串数组转换为包含键值对的对象数组。下面是我目前对它的看法 Mongo记录: 同样的mongo记录在我下面的第一个问题中 当前查询: templateAttributes.find({platform:"V1"}).map(function(c){ //instantiate a new array var optionsArray = []; for (var i=0;i&

感谢@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"}]
db.templateAttributes.find({ platform: "A1" }, { "available.Community.attributes.type.defaultValue": 1 }).toArray()[0]['available']['Community']['attributes']['type']['defaultValue']
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
[
    {
            "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 }
)
但它们似乎都检索到了整个Hirachy对象,如下所示:

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

你可以试试下面的方法

使用查找:

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"}]
db.templateAttributes.find({ platform: "A1" }, { "available.Community.attributes.type.defaultValue": 1 }).toArray()[0]['available']['Community']['attributes']['type']['defaultValue']
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
[
    {
            "label" : "well-known",
            "value" : "well-known"
    },
    {
            "label" : "simple",
            "value" : "simple"
    },
    {
            "label" : "complex",
            "value" : "complex"
    }
]
使用findOne:

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"}]
db.templateAttributes.find({ platform: "A1" }, { "available.Community.attributes.type.defaultValue": 1 }).toArray()[0]['available']['Community']['attributes']['type']['defaultValue']
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
[
    {
            "label" : "well-known",
            "value" : "well-known"
    },
    {
            "label" : "simple",
            "value" : "simple"
    },
    {
            "label" : "complex",
            "value" : "complex"
    }
]
使用聚合:

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"}]
db.templateAttributes.find({ platform: "A1" }, { "available.Community.attributes.type.defaultValue": 1 }).toArray()[0]['available']['Community']['attributes']['type']['defaultValue']
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
[
    {
            "label" : "well-known",
            "value" : "well-known"
    },
    {
            "label" : "simple",
            "value" : "simple"
    },
    {
            "label" : "complex",
            "value" : "complex"
    }
]
输出:

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"}]
db.templateAttributes.find({ platform: "A1" }, { "available.Community.attributes.type.defaultValue": 1 }).toArray()[0]['available']['Community']['attributes']['type']['defaultValue']
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
[
    {
            "label" : "well-known",
            "value" : "well-known"
    },
    {
            "label" : "simple",
            "value" : "simple"
    },
    {
            "label" : "complex",
            "value" : "complex"
    }
]
编辑:回答更新的问题:请在此处使用聚合

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
输出:

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"}]
db.templateAttributes.find({ platform: "A1" }, { "available.Community.attributes.type.defaultValue": 1 }).toArray()[0]['available']['Community']['attributes']['type']['defaultValue']
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
[
    {
            "label" : "well-known",
            "value" : "well-known"
    },
    {
            "label" : "simple",
            "value" : "simple"
    },
    {
            "label" : "complex",
            "value" : "complex"
    }
]

我对mongo还是个新手。我真的不需要更改文档的任何结构。我只对只获取该字段值的查询感兴趣。我已经有了map方法,但我只是想知道是否有更好的方法it@blueren:更新了我的答案。太棒了!谢谢这就是我要找的。但是我想了解一下它是如何工作的。Find返回一个游标,您将它转换为arary并拾取第0个元素?芬顿也是一样?@blueren:我的答案中也有芬顿的代码。使用findOne,您将收到单个文档(而不是光标)。因此,无需转换为数组并访问第0个元素。您可以直接访问字段。谢谢@4J41,我有一个后续问题。我已经更新了主要问题。