管道将属性添加到数组中的每个文档
Pipeline to add an attribute to each document in an array
我需要一种方法来为聚合管道中文档数组中的每个文档添加一个属性。属性的值来自数组中的值。我的 collection 有这样的行:
{'a': 1, 'b': [{ 'this': 'A', 'that': 'B', 'other': 'C' }], 'c': 2},
{'a': 3, 'b': [{ 'this': 'D', 'that': 'E', 'other': 'F' }, {'this': 'G', 'that': 'H', 'other': 'I'}], 'c': 4}
我想将每个 'b' 转换为具有两个属性 'foo' 和 'bar' 的文档数组。 'b' 的每个元素将变为 'foo'。 'bar' 的值是一个 object,其属性值来自 'foo' 的 'A' 属性。
我不想将结果写回 collection。
以下是我要进行的转换:
{ "a" : 1, "b" : [ { "bar": { "this_value": "A"}, "foo" : { "this" : "A", "that" : "B", "other" : "C" } } ], "c" : 2 }
{ "a" : 3, "b" : [ { "bar": { "this_value": "D"}, "foo" : { "this" : "D", "that" : "E", "other" : "F" } }, { "bar" : { "this_value": "G" }, "foo" : { "this" : "G", "that" : "H", "other" : "I" } } ], "c" : 4 }
In 如何在 MongoDB 中将数组字段的元素更改为具有单个属性的字典的值
,我学会了如何构建 "foo" 属性:
t.aggregate( [{$addFields: {'b': { $map: { input: '$b', in: {'foo': '$$this'}}}}} ] )
我被困在如何添加 "bar" 属性上。
下面是 Mongo Shell 命令来创建我的 collection 和 "foo" 转换部分:
c = new Mongo()
db = c.getDB('playful')
t = db['things']
t.insertMany([{'a': 1, 'b': [{ 'this': 'A', 'that': 'B', 'other': 'C' }], 'c': 2}, {'a': 3, 'b': [{ 'this': 'D', 'that': 'E', 'other': 'F' }, {'this': 'G', 'that': 'H', 'other': 'I'}], 'c': 4}])
t.aggregate( [{$addFields: {'b': { $map: { input: '$b', in: {'foo': '$$this'}}}}} ] )
我在 Ubuntu 18.04 上使用 MongoDB 4.2.2。
只需在为 foo 做的地方构建 bar 对象:
db.collection.aggregate([
{
$addFields: {
"b": {
$map: {
input: "$b",
in: {
"foo": "$$this",
"bar": {
this_value: "$$this.this"
}
}
}
}
}
}
])
你可以测试一下here
我需要一种方法来为聚合管道中文档数组中的每个文档添加一个属性。属性的值来自数组中的值。我的 collection 有这样的行:
{'a': 1, 'b': [{ 'this': 'A', 'that': 'B', 'other': 'C' }], 'c': 2},
{'a': 3, 'b': [{ 'this': 'D', 'that': 'E', 'other': 'F' }, {'this': 'G', 'that': 'H', 'other': 'I'}], 'c': 4}
我想将每个 'b' 转换为具有两个属性 'foo' 和 'bar' 的文档数组。 'b' 的每个元素将变为 'foo'。 'bar' 的值是一个 object,其属性值来自 'foo' 的 'A' 属性。
我不想将结果写回 collection。
以下是我要进行的转换:
{ "a" : 1, "b" : [ { "bar": { "this_value": "A"}, "foo" : { "this" : "A", "that" : "B", "other" : "C" } } ], "c" : 2 }
{ "a" : 3, "b" : [ { "bar": { "this_value": "D"}, "foo" : { "this" : "D", "that" : "E", "other" : "F" } }, { "bar" : { "this_value": "G" }, "foo" : { "this" : "G", "that" : "H", "other" : "I" } } ], "c" : 4 }
In 如何在 MongoDB 中将数组字段的元素更改为具有单个属性的字典的值 ,我学会了如何构建 "foo" 属性:
t.aggregate( [{$addFields: {'b': { $map: { input: '$b', in: {'foo': '$$this'}}}}} ] )
我被困在如何添加 "bar" 属性上。
下面是 Mongo Shell 命令来创建我的 collection 和 "foo" 转换部分:
c = new Mongo()
db = c.getDB('playful')
t = db['things']
t.insertMany([{'a': 1, 'b': [{ 'this': 'A', 'that': 'B', 'other': 'C' }], 'c': 2}, {'a': 3, 'b': [{ 'this': 'D', 'that': 'E', 'other': 'F' }, {'this': 'G', 'that': 'H', 'other': 'I'}], 'c': 4}])
t.aggregate( [{$addFields: {'b': { $map: { input: '$b', in: {'foo': '$$this'}}}}} ] )
我在 Ubuntu 18.04 上使用 MongoDB 4.2.2。
只需在为 foo 做的地方构建 bar 对象:
db.collection.aggregate([
{
$addFields: {
"b": {
$map: {
input: "$b",
in: {
"foo": "$$this",
"bar": {
this_value: "$$this.this"
}
}
}
}
}
}
])
你可以测试一下here