Query document by param matching an array of ids

I need to select only the documents that mach my ID inside an array of IDs

I have an ID of navigations “03096f53-c963-4f38-959e-d8149753cab3” and I need to get all pages that has an navigations with that ID … an example in mongoplayground

I have navigations in the properties header, body and footer.

  "pages": [
    {
      "_id": "7ae38dbd-50c6-4a52-bd87-512e1a9640f0",
      "footer": {
        "sections": [
          {
            "navigations": []
          }
        ]
      },
      "body": {
        "sections": [
          {
            "navigations": []
          }
        ]
      },
      "header": {
        "sections": [
          {
            "navigations": [
              "03096f53-c963-4f38-959e-d8149753cab3"
            ]
          }
        ]
      },
      "title": "My Page 1",
      "__v": 0
    },
    {
      "_id": "7ae38dbd-50c6-4a52-bd87-512e1a9640f1",
      "footer": {
        "sections": [
          {
            "navigations": []
          }
        ]
      },
      "body": {
        "sections": [
          {
            "navigations": [
              "03096f53-c963-4f38-959e-d8149753cab3"
            ]
          }
        ]
      },
      "header": {
        "sections": [
          {
            "navigations": []
          }
        ]
      },
      "title": "My Page 2",
      "__v": 0
    },
    {
      "_id": "7ae38dbd-50c6-4a52-bd87-512e1a9640f2",
      "footer": {
        "sections": [
          {
            "navigations": [
              "03096f53-c963-4f38-959e-d8149753cab3"
            ]
          }
        ]
      },
      "body": {
        "sections": [
          {
            "navigations": []
          }
        ]
      },
      "header": {
        "sections": [
          {
            "navigations": []
          }
        ]
      },
      "title": "My Page 3",
      "__v": 0
    },
    {
      "_id": "7ae38dbd-50c6-4a52-bd87-512e1a9640f3",
      "footer": {
        "sections": [
          {
            "navigations": []
          }
        ]
      },
      "body": {
        "sections": [
          {
            "navigations": []
          }
        ]
      },
      "header": {
        "sections": [
          {
            "navigations": []
          }
        ]
      },
      "title": "My Page 4",
      "__v": 0
    }
  ]
}

with this query I get my pages

db.pages.aggregate([
  {
    $match: {
      $or: [
        {
          "header.sections.navigations": "03096f53-c963-4f38-959e-d8149753cab3"
        },
        {
          "body.sections.navigations": "03096f53-c963-4f38-959e-d8149753cab3"
        },
        {
          "footer.sections.navigations": "03096f53-c963-4f38-959e-d8149753cab3"
        }
      ]
    }
  },
  {
    "$project": {
      _id: 1,
      title: 1
    }
  }
])

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.