识别在a.d中的记录,这些记录在嵌套数组a.p中没有相应的记录。

huangapple go评论66阅读模式
英文:

Identify records in a.d that do not have corresponding record in a.p in nested array

问题

I can provide a translation of the requested text without any additional content. Here is the translated text:

请帮助,我需要识别在a.p[]中没有重复的a.d[]数组记录的列表。在下面的示例中,具有a.d.prid:"2"的嵌套对象就是这样一条记录:它与a.p[pid]和a.d[pid]具有相同的pid,但prid不同="2"

示例文档:

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

我需要一个包含所有这样的a.d记录的列表,这些记录在a.d[]和a.p[]中具有相同的pid,但具有不同的prid。

期望的输出:

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

稍后我需要删除这些嵌套对象,因为它们被视为"损坏的",但首先需要获取列表...

playground

英文:

Please, help , I need to identify the list of array records a.d[] that do not have duplicate in a.p[] . in the example below nested object 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

得分: 1

以下是您要翻译的内容:

一种选择是在$map内使用$mergeObjects$in来查找d上没有与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}}}
])

更新: 由于某种原因,它在mongodb playground上不一致,但我不明白为什么。经过测试,在Robo 3T和Studio 3T上是一致的...

英文:

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

huangapple
  • 本文由 发表于 2023年5月22日 17:15:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定