聚合管道中的嵌入式文档集合中的参考文档字段

雷诺C.

我正在尝试在管道阶段合并位于嵌入式文档集合中的两个数组字段。但是我坚持如何引用嵌入式文档的两个“内部”数组。

集合

[{
    name: "first",
    docs: [
        { a1: ["a", "b"], a2: ["c"] },
        { a1: ["d", "e"], a2: ["f"] }
    ]
},
{
    name: "second",
    docs: [
        { a1: [1, 2], a2: [3] },
        { a1: [4, 5], a2: [6] }
    ]
}]

预期结果

[{
    name: "first",
    docs: [
        { merged: ["a", "b", "c"] },
        { merged: ["d", "e", "f"] }
    ]
},
{
    name: "second",
    docs: [
        { merged: [1, 2, 3] },
        { merged: [4, 5, 6] }
    ]
}]

方法

到目前为止,我尝试的总体方法是:(具有2个硬编码数组用于测试目的)

db.getCollection("collection").aggregate([{
    $set: {
         "docs.merged": {
             $concatArrays: [["hello"], ["world"]]
         }
    }
}])

产生预期的结果:

[{
    name : "first",
    docs : [
        {
            a1 : ["a", "b"],
            a2 : ["c"],
            merged : ["hello", "world"] // <- OK
        },
        {
            a1 : ["d", "e"],
            a2 : ["f"],
            merged : ["hello", "world"] // <- OK
        }
    ]
},{
    name : "second",
    docs : [
        {
            a1 : [1.0, 2.0],
            a2 : [3.0],
            merged : ["hello", "world"] // <- OK
        },
        {
            a1 : [4.0, 5.0],
            a2 : [6.0],
            merged : ["hello", "world"] // <- OK
        }
    ]
}]

但是我很难理解如何引用当前嵌入式文档中的字段

// Using the "$" reference causes following error:
// Invalid $set :: caused by :: FieldPath field names may not start with '$'.
{
    $set: {
         "docs.merged": { $concatArrays: ["$docs.$.a1", "$docs.$.a2"] }
    }
}

// $$this is only available with a MAP operator
{
    $set: {
         "docs.merged": { $concatArrays: ["$$this.a1", "$$this.a2"] }
    }
}

注意事项

我不能使用update查询,因为不得更改原始文档。因此,这必须aggregate管道中实现

unwind在这一点上,我尝试避免使用操作,因为这会对性能产生重大影响。实际的文件中含有相当多的(变量)字段中的它的根; groupunwind相当复杂阶段之后登台表演(示例已大大简化了可读性)

我正在使用MongoDB v4.4

Rfroes87

我想这样做会成功,请让我知道我是否缺少任何东西:

db.collection.aggregate([{
   $project: {
      _id: 0,
      "name": 1,
      "docs": {
         $function: {
            body: function(docs) {
              docs.forEach(function(doc) {
                 var merged = [];
                 Object.keys(doc).forEach(function(k) {
                    merged = merged.concat(doc[k]);
                    delete doc[k];
                 });
                 doc.merged = merged;
              });
              return docs;
            },
            args: [ "$docs" ],
            lang: "js"
         }
      }
   }
}])

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从mongoDB中的手动参考中查找所有嵌入式文档

mgo,mongodb:从嵌入式结构中查找与一个字段匹配的文档

Spring Data MongoDB-嵌入式文档在其他文档中作为参考

将嵌入式文档插入到mongodb文档中的新字段

计算嵌入式文档/数组中字段的平均值

mongo db中嵌入式文档中的条件聚合

Mongo查询嵌入式文档中的动态字段

通过MongoDB中的聚合框架将嵌入式对象作为文档检索

根据mongoDB聚合中的条件,项目嵌入式文档密钥值

mongodb聚合中的项目嵌套嵌入式文档

从嵌入式文档中删除字段

在MongoDB中排除嵌入式文档中除单个字段之外的所有字段

如何在数组中的嵌入式文档中指定/投影字段?

MongoDB-在嵌入式文档中找不到特定字段

按嵌入式文档字段排序

对于整个MongoDB集合,将嵌入式文档的字段放在其父级中的最有效方法是什么?

MongoDB C#驱动程序-更新嵌入式文档数组中的所有字段

是否有任何查询通过检查mongoDb中嵌套的嵌入式文档的字段来查找文档

MongoDB聚合:通过动态字段路径从嵌入式文档添加字段

Spring Data Mongo-在嵌入式文档中应用唯一的组合字段

Mongo嵌入式文档字段查询

查询MongoDB中的嵌入式文档

MongoDB嵌入式文档参考了其他嵌入式文档

嵌入式文档中的批量更新

在mongodb中展开嵌入式文档

使用C#在MongoDB集合中的嵌入式文档的子句查询中的投影

Mongodb:将字段添加到嵌入式文档中(无数组)

Mongodb 聚合管道,用于匹配集合中子文档的参考模型字段名称

在 MongoDB 中搜索嵌入式文档?