1

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 ...

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

I have related new challenge here: stackoverflow.com/questions/74055077/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.