Home > Net >  Mongodb: How to merge $facet output 2 by 2?
Mongodb: How to merge $facet output 2 by 2?

Time:10-31

Playground:https://mongoplayground.net/p/YUV_fReyGsr I have following query. I need to combine the result 2 by 2. Meaning I need to combine facet "1","2" as a result and facet "3","4" as another result. It's guaranteed that the number of facet will be even. Also, each pair of facet should get at most one record(it might not matter)

db.collection.aggregate([
  {
    "$facet": {
      "1": [
        {
          $match: {
            "ID": "2"
          }
        }
      ],
      "2": [
        {
          $match: {
            "array.ID": "2"
          }
        }
      ],
      "3": [
        {
          $match: {
            "array.ID": "4"
          }
        }
      ],
      "4": [
        {
          $match: {
            "ID": "4"
          }
        }
      ]
    }
  }
])

The expected result will be

[
  {
    "1": [
      {
        "ID": "1",
        "array": [
          {
            "ID": "2",
            "attribute1": "456"
          },
          {
            "ID": "3",
            "attribute1": "567"
          }
        ],
        "attr1": "123"
      }
    ],
    "2": [
      {
        "ID": "4",
        "array": [
          {
            "ID": "5",
            "attr1": "456"
          }
        ],
        "attr1": "123"
      }
    ]
  }
]

CodePudding user response:

I was able to figure this out using $concatArrays operator, along with $project.

Live demo here

Database

[
  {
    "ID": "1",
    "attr1": "123",
    "array": [
      {
        "ID": "2",
        "attribute1": "456"
      },
      {
        "ID": "3",
        "attribute1": "567"
      }
    ]
  },
  {
    "ID": "4",
    "attr1": "123",
    "array": [
      {
        "ID": "5",
        "attr1": "456"
      }
    ]
  }
]

Query

db.collection.aggregate([
  {
    "$facet": {
      "1": [
        {
          $match: {
            "ID": "2"
          }
        }
      ],
      "2": [
        {
          $match: {
            "array.ID": "2"
          }
        }
      ],
      "3": [
        {
          $match: {
            "array.ID": "4"
          }
        }
      ],
      "4": [
        {
          $match: {
            "ID": "4"
          }
        }
      ]
    }
  },
  {
    "$project": {
      _id: 0,
      "1": {
        "$concatArrays": [
          "$1",
          "$2"
        ]
      },
      "2": {
        "$concatArrays": [
          "$3",
          "$4"
        ]
      }
    }
  }
])

Result

[
  {
    "1": [
      {
        "ID": "1",
        "_id": ObjectId("5a934e000102030405000000"),
        "array": [
          {
            "ID": "2",
            "attribute1": "456"
          },
          {
            "ID": "3",
            "attribute1": "567"
          }
        ],
        "attr1": "123"
      }
    ],
    "2": [
      {
        "ID": "4",
        "_id": ObjectId("5a934e000102030405000001"),
        "array": [
          {
            "ID": "5",
            "attr1": "456"
          }
        ],
        "attr1": "123"
      }
    ]
  }
]
  • Related