'MongoDB Atlas - aggregate pipeline an collection and store the result into another

I have an XML DB in Basex that contains information relative to x number of visits. I need to return via postman the xml code to be transformed to JSON via xml2json and then stored in mongoDB Atlas.

Postman returns this code

    {
    "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"
                }]
            }
        }]
    }
}

After that I insert into a db in MongoDB Atlas using the MongoDB Data API.

Now is where the problems start:

  • I dont know to unwind "visitas.visita" in Atlas and store it in another collection like I did using mongosh.


Solution 1:[1]

If I understand correctly, I think you're looking for an aggregation pipeline with $out (https://docs.mongodb.com/manual/reference/operator/aggregation/out/)

For example:

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

Playground: https://mongoplayground.net/p/j9_foHeaKof

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 emragins