1

I need to identify the list of array records a.d[] that do not have duplicate in a.p[]. In the nested object below with a.d.prid:"2" is such an record: it has same pid with a.p[pid] and a.d[pid], but has the prid different="2"

Example document:

{
 "f": 1,
 a: [
  { 
    aid:1,
    p: [
      {
        prid: "1",
        pid: {
          c: "A",
          s: "B",
          k: "C"
        },
        X: 20
      }
    ],
    d: [
      {
        prid: "1",
        pid: {
          c: "A",
          s: "B",
          k: "C"
        }
      },
      {
        prid: "2",
        pid: {
          c: "A",
          s: "B",
          k: "C"
        }
      }
    ]
  }
  ]
 }

I need a list with all such a.d records that have pid for same document in a.d[] and a.p[] , but have different prid

Expected output:

{ "f":1 , "aid": 1 , "prid": 2 }

playground

Later I need to delete those nested objects as they are considered "corrupted", but need to get the list first.

1 Answer 1

1

One option is to use $mergeObjects and $in inside a $map to find the items on d without a matching item on p:

db.collection.aggregate([
  {$set: {
      a: {$map: {
          input: "$a",
          as: "item",
          in: {
            d: "$$item.d",
            aid: "$$item.aid",
            p: {$map: {
                input: "$$item.p",
                in: {$mergeObjects: [{prid: "$$this.prid"}, "$$this.pid"]}
            }}
      }}}
  }},
  {$project: {
      _id: 0,
      res: {$map: {
          input: "$a",
          as: "item",
          in: {$reduce: {
              input: "$$item.d",
              initialValue: [],
              in: {$concatArrays: [
                  "$$value",
                  {$cond: [
                      {$in: [
                          {$mergeObjects: [
                              {prid: "$$this.prid"},
                              "$$this.pid"
                          ]},
                          "$$item.p"
                      ]},
                      [],
                      [{
                          f: "$f",
                          aid: "$$item.aid",
                          prid: "$$this.prid"
                      }]
                  ]}
              ]}
          }}
      }}
  }},
  {$set: {
      res: {$reduce: {
          input: "$res",
          initialValue: [],
          in: {$concatArrays: ["$$value", "$$this"]}
      }}
  }},
  {$match: {"res.0": {$exists: 1}}}
])

Update: For some reason it is not consistent on the mongodb playground, but I don't understand why. It is tested and consistent on Robo 3T and studio 3T...

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

2 Comments

Thanks @nimrod , it seems something is not correct, in the playground the aid:2 d.prid:7 is matching the condition but not shown in the result ... , the result need to show only the ones from d not matching in p
@R2D2, For some reason it is not consistent on the mongodb playground, but I don't understand why. It is tested and consistent on Robo 3T and studio 3T...

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.