【问题标题】:MongoDB Atlas - aggregate pipeline an collection and store the result into anotherMongoDB Atlas - 集合中的聚合管道并将结果存储到另一个中
【发布时间】:2022-01-30 13:22:07
【问题描述】:

我在 Basex 中有一个 XML DB,其中包含与 x 访问次数相关的信息。 我需要通过邮递员返回要通过 xml2json 转换为 JSON 的 xml 代码,然后存储在 mongoDB Atlas 中。

邮递员返回此代码

    {
    "visitas": {
        "visita": [{
            "id": "4fc7900e-8e9d-432e-bf38-e9c0b5a10cd9",
            "data": "2021-09-16",
            "morada": {
                "pais": "Portugal",
                "cidade_origem": "Penafiel"
            },
            "pessoas": {
                "pessoa": [{
                    "nome": "pessoa1",
                    "data_nascimento": "2021-12-29"
                }, {
                    "nome": "pessoa2",
                    "data_nascimento": "2021-12-29"
                }, {
                    "nome": "pessoa3",
                    "data_nascimento": "2021-12-29"
                }]
            }
        }, {
            "id": "943c0e88-3eda-48ef-8105-6c48cde093a7",
            "data": "2021-09-18",
            "morada": {
                "pais": "Portugal",
                "cidade_origem": "Penafiel"
            },
            "pessoas": {
                "pessoa": [{
                    "nome": "pessoa1",
                    "data_nascimento": "2021-12-29"
                }, {
                    "nome": "pessoa2",
                    "data_nascimento": "2021-12-29"
                }, {
                    "nome": "pessoa3",
                    "data_nascimento": "2021-12-29"
                }]
            }
        }, {
            "id": "877e1108-251e-410c-95e3-8df5dbfbe4e2",
            "data": "2021-09-18",
            "morada": {
                "pais": "Portugal",
                "cidade_origem": "Penafiel"
            },
            "pessoas": {
                "pessoa": [{
                    "nome": "pessoa1",
                    "data_nascimento": "2021-12-29"
                }, {
                    "nome": "pessoa2",
                    "data_nascimento": "2021-12-29"
                }, {
                    "nome": "pessoa3",
                    "data_nascimento": "2021-12-29"
                }]
            }
        }]
    }
}

之后,我使用 MongoDB 数据 API 插入 MongoDB Atlas 中的数据库。

现在是问题开始的地方:

  • 我不知道在 Atlas 中展开“visitas.visita”并将其存储在 另一个集合,就像我使用 mongosh 所做的那样。

【问题讨论】:

  • 我有一个解决方案,但要让它发挥作用,您可以直接连接到 MongoDB 实例吗?

标签: mongodb mongodb-atlas


【解决方案1】:

如果我理解正确,我认为您正在寻找带有 $out (https://docs.mongodb.com/manual/reference/operator/aggregation/out/) 的聚合管道

例如:

db.collection.aggregate([
  {
    "$unwind": "$visitas.visita"
  },
  {
    "$out": {
      "db": "output-db",
      "coll": "output-collection"
    }
  }
])

游乐场:https://mongoplayground.net/p/j9_foHeaKof

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 2020-08-31
    • 2014-01-25
    • 2013-06-17
    • 1970-01-01
    • 2020-01-12
    • 2018-10-19
    相关资源
    最近更新 更多