使用键值对将mongo数组转换为对象 [英] Converting mongo array to object with key-value pair

查看:170
本文介绍了使用键值对将mongo数组转换为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个mongo文档,其中包含一个字符串数组,并且需要将此特定的字符串数组转换为包含键值对的对象数组.以下是我目前的解决方案.

I have a mongo document that contains an array of Strings, and I need to convert this particular array of strings into an array of object containing a key-value pair. Below is my curent appraoch to it.

{
    "_id" : ObjectId("57e3720836e36f63695a2ef2"),
    "platform" : "A1",
    "available" : {
        "Community" : {
            "attributes" : {
                "type" : {
                    "values" : [
                        "well-known",
                        "simple",
                        "complex"
                    ],
                    "defaultValue" : "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"}]

我的方法是否足够有效,还是有一种方法可以优化上述查询以获得相同的预期结果?

Is my approach efficient enough, or is there a way to optimize the above query to get the same desired result?

推荐答案

由于键和值都相同,因此不确定要对最终结果做什么.但是,您可以使用聚合框架 您可以在其中使用 $unwind 运算符将其展平,即,它为每个数组条目生成每个文档的副本.

Not so sure what you want to do with the end result as the keys and the values are just the same. Nonetheless, you can use the aggregation framework where you can denormalise the embedded values array by using the $unwind operator which flattens it i.e. it produces a copy of each document per array entry.

将值数组展平后,您可以应用 $project 运算符会将上一个分组中的字段整形为所需的格式.

After flattening the values array, you can apply the $group accumulation operators on the values to aggregate them. A final pipeline of the $project operator would shape the fields from the previous grouping into the desired format.

按照以下示例获得概念:

Follow this example to get the concept:

templateAttributes.aggregate([
    { "$match": { "platform": "V1" } },
    { "$unwind": "$available.Community.attributes.type.values" },
    {
        "$group": {
            "_id": "$available.Community.attributes.type.values",
            "value": { "$first": "$available.Community.attributes.type.values" }
        }
    },
    {
        "$project": {
            "_id": 0,
            "label": "$_id",
            "value": 1
        }
    }
])


由于您正在使用流星,所以添加流星 meteorhacks:aggregate 适当的Meteor聚合支持,以便您可以在集合上运行上述聚合管道.


Since you are using Meteor, meteor add meteorhacks:aggregate will add proper aggregation support for Meteor so that you can run the above aggregation pipeline on your collection.

这篇关于使用键值对将mongo数组转换为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆