将 mongo 数组转换为具有键值对的对象
Converting mongo array to object with key-value pair
我有一个包含字符串数组的 mongo 文档,我需要将这个特定的字符串数组转换为包含键值对的对象数组。以下是我目前的做法。
{
"_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"}]
我的方法是否足够有效,或者是否有优化上述查询以获得相同期望结果的方法?
这里请使用聚合。
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"
}
]
不太确定你想对最终结果做什么,因为键和值是一样的。尽管如此,您可以使用聚合框架
您可以在其中使用 $unwind
运算符对嵌入值数组进行非规范化,该运算符会将其展平,即它会为每个数组条目生成每个文档的副本。
展平值数组后,您可以应用 $group
accumulation operators on the values to aggregate them. A final pipeline of the $project
运算符将先前分组的字段调整为所需格式。
按照这个例子来理解概念:
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
}
}
])
由于您使用的是 Meteor,meteor add meteorhacks:aggregate
将为 Meteor 添加适当的聚合支持,以便您可以 运行 在您的collection.
使用查询
templateAttributes.aggregate([{
$match: {
"platform" : "A1"
}},
{
$unwind: {path : "$available.Community.attributes.type.values"}},{ $group:{
_id:"$_id",
result:{
$push: {
label: "$available.Community.attributes.type.values", value: "$available.Community.attributes.type.values"
}
}
}}])
它会给你准确的答案。
我有一个包含字符串数组的 mongo 文档,我需要将这个特定的字符串数组转换为包含键值对的对象数组。以下是我目前的做法。
{
"_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"}]
我的方法是否足够有效,或者是否有优化上述查询以获得相同期望结果的方法?
这里请使用聚合。
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"
}
]
不太确定你想对最终结果做什么,因为键和值是一样的。尽管如此,您可以使用聚合框架
您可以在其中使用 $unwind
运算符对嵌入值数组进行非规范化,该运算符会将其展平,即它会为每个数组条目生成每个文档的副本。
展平值数组后,您可以应用 $group
accumulation operators on the values to aggregate them. A final pipeline of the $project
运算符将先前分组的字段调整为所需格式。
按照这个例子来理解概念:
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
}
}
])
由于您使用的是 Meteor,meteor add meteorhacks:aggregate
将为 Meteor 添加适当的聚合支持,以便您可以 运行 在您的collection.
使用查询
templateAttributes.aggregate([{
$match: {
"platform" : "A1"
}},
{
$unwind: {path : "$available.Community.attributes.type.values"}},{ $group:{
_id:"$_id",
result:{
$push: {
label: "$available.Community.attributes.type.values", value: "$available.Community.attributes.type.values"
}
}
}}])
它会给你准确的答案。