Home > database >  Double nested array with multiple nested documents identify wrong value
Double nested array with multiple nested documents identify wrong value

Time:10-13

I need to identify which documents have the wrong date string ( $gt:10 characters) from all my collection :

  {
    "_id": ObjectId("5c05984246a0201286d4b57a"),
    f: "x",
    "_a": [
      {
        "_onlineStore": {}
      },
      {
        "_p": {
          "s": {
            "a": {
              "t": [
                {
                  "dateP": "20200-09-20",
                  "l": "English",
                  "size": "XXL"
                }
              ]
            },
            "c": {
              "t": [
                {
                  "dateP": "20300-09-20",
                  "l": "English",
                  "size": "XXL"
                }
              ]
            }
          }
        }
      }
    ]
  }

and output need to be as follow:

{f:"x",dateP:"20200-09-20", t:"c"}
{f:"x",dateP:"20300-09-20", t:"a"}

The last field in the output "t" not compulsory but desirable ... Please, help ...

CodePudding user response:

We can use $objectToArray for this:

db.collection.aggregate([
  {$unwind: "$_a"},
  {$project: {_id: 0, f: 1, data: {$objectToArray: "$_a._p.s"}}},
  {$unwind: "$data"},
  {$unwind: "$data.v.t"},
  {$match: {$expr: {$gt: [{$strLenCP: "$data.v.t.dateP"}, 10]}}},
  {$project: {f: 1, dateP: "$data.v.t.dateP", t: "$data.k"}}
])

See how it works on the playground example

  • Related