Home > Enterprise >  How to get only specific nested object from MongoDB collection where uuid is used as key
How to get only specific nested object from MongoDB collection where uuid is used as key

Time:09-03

I only want this matched object in query result, by querying uuid "630c260e333c41549c43cae5f8e626d5":

        {
          "enableAnnotations": false,
          "enableMultipleFilesUpload": true,
          "enableWorkflows": true,
          "formSectionID": "a9bcde7f8fc14e519965a655d9574fb7",
          "isThumbnailField": false,
          "label": "WD multi upload Ttl in Dup Section DW",
          "publishToFolder": false,
          "publishWhenApproved": false,
          "type": "upload",
          "uuid": "630c260e333c41549c43cae5f8e626d5"
        }

from the following Collection document:

[{
  "_id": "07672b703cc44ce6887159984911dd6e",
  "createTime": "2022-06-21T12:08:00Z",
  "creator": "75e5d59fa1be0f932bff8407536730d0",
  "defination": {
    "fields": {
      "a9bcde7f8fc14e519965a655d9574fb7": [
        {
          "enableAnnotations": false,
          "enableMultipleFilesUpload": true,
          "enableWorkflows": true,
          "formSectionID": "a9bcde7f8fc14e519965a655d9574fb7",
          "isThumbnailField": false,
          "label": "WD multi upload Ttl in Dup Section DW",
          "publishToFolder": false,
          "publishWhenApproved": false,
          "type": "upload",
          "uuid": "630c260e333c41549c43cae5f8e626d5"
        },
        {
          "enableAnnotations": false,
          "enableMultipleFilesUpload": false,
          "formSectionID": "a9bcde7f8fc14e519965a655d9574fb7",
          "isThumbnailField": false,
          "label": "WD single upload in Dup section DW",
          "publishToFolder": false,
          "type": "upload",
          "uuid": "1aecb33709434d9b9a0cb455c67e2295"
        }
      ],
      "eda3ce9883a14b948eae5b053f720227": [
        {
          "enableAnnotations": false,
          "enableMultipleFilesUpload": true,
          "enableWorkflows": true,
          "formSectionID": "eda3ce9883a14b948eae5b053f720227",
          "isThumbnailField": false,
          "label": "WD upload multi",
          "publishToFolder": false,
          "publishWhenApproved": false,
          "type": "upload",
          "uuid": "e88a097621cc44c4bb440902006f5a12"
        },
        {
          "formSectionID": "eda3ce9883a14b948eae5b053f720227",
          "label": "WD Txt Fld Ttl",
          "type": "text",
          "uuid": "e1bf361509364718b9d52e65cbc292de",
        },
        {
          "enableAnnotations": false,
          "enableMultipleFilesUpload": true,
          "enableWorkflows": true,
          "formSectionID": "eda3ce9883a14b948eae5b053f720227",
          "isThumbnailField": false,
          "label": "WD 2nd-multi-upload",
          "publishToFolder": false,
          "publishWhenApproved": false,
          "type": "upload",
          "uuid": "fbee1d1adf224712a90f3f37b8395aa0"
        },
        {
          "enableAnnotations": true,
          "enableMultipleFilesUpload": false,
          "formSectionID": "eda3ce9883a14b948eae5b053f720227",
          "isThumbnailField": false,
          "label": "WD single upload Ttl DW",
          "publishToFolder": false,
          "type": "upload",
          "uuid": "5504ce66d1bc4f659eefed4df4e73b99",
        }
      ]
    },
    "sections": {
      "291b58eddee04a05bd8e7d80d99241b9": [
        {
          "uuid": "eda3ce9883a14b948eae5b053f720227"
        },
        {
          "uuid": "a9bcde7f8fc14e519965a655d9574fb7"
        }
      ]
    },
    "tabs": [
      {
        "uuid": "291b58eddee04a05bd8e7d80d99241b9"
      }
    ]
  }
}]

After some research, I used the query: (by referring how to select value if key is uuid in mongodb)

db.collection.aggregate([
  {
    "$set": {
      "fields": {
        "$objectToArray": "$defination.fields"
      }
    }
  },
  {
    "$unwind": "$fields"
  },
  {
    "$match": {
      "fields.v.uuid": "630c260e333c41549c43cae5f8e626d5",
      "fields.v.enableWorkflows": true,
      "fields.v.enableMultipleFilesUpload": true,
      "fields.v.type": "upload"
    }
  },
  {
    "$group": {
      "_id": "$_id",
      "fields": {
        "$push": "$fields"
      }
    }
  },
  {
    "$set": {
      "fields": {
        "$arrayToObject": "$fields"
      }
    }
  }
])

and I got the result:

[
  {
    "_id": "07672b703cc44ce6887159984911dd6e",
    "fields": {
      "a9bcde7f8fc14e519965a655d9574fb7": [
        {
          "enableAnnotations": false,
          "enableMultipleFilesUpload": true,
          "enableWorkflows": true,
          "formSectionID": "a9bcde7f8fc14e519965a655d9574fb7",
          "isThumbnailField": false,
          "label": "WD multi upload Ttl in Dup Section DW",
          "publishToFolder": false,
          "publishWhenApproved": false,
          "type": "upload",
          "uuid": "630c260e333c41549c43cae5f8e626d5"
        },
        {
          "enableAnnotations": false,
          "enableMultipleFilesUpload": false,
          "formSectionID": "a9bcde7f8fc14e519965a655d9574fb7",
          "isThumbnailField": false,
          "label": "WD single upload in Dup section DW",
          "publishToFolder": false,
          "type": "upload",
          "uuid": "1aecb33709434d9b9a0cb455c67e2295"
        }
      ]
    }
  }
]

But it contains extra sibling objects.

I tried the answer from post but it doesn't work: How can I get only specific object from nested array mongodb

CodePudding user response:

One option to drill down is using $reduce with $filter, but first we need known keys, hence the $objectToArray which you already have:

db.collection.aggregate([
  {$project: {fields: {$objectToArray: "$defination.fields"}}},
  {$project: {res: {
        $first: {
          $reduce: {
            input: "$fields",
            initialValue: [],
            in: {$concatArrays: [
                "$$value",
                {$filter: {
                    input: "$$this.v",
                    as: "inner",
                    cond: {
                      $eq: [
                        "$$inner.uuid",
                        "630c260e333c41549c43cae5f8e626d5"
                      ]
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  },
  {$replaceRoot: {newRoot: "$res"}}
])

See how it works on the playground example

  • Related