-1

Hello sorry i am new in with nosql and mongodb please kindly help me here is my sample data

[
  {
    "_id": ObjectId("611532b19c6e77ad0fd50c1c"),
    "status": true,
    "role_name": "DEVELOPER",
    "descr": "DEVELOPER",
    "menu": [
      {
        "permissions": [
          ObjectId("60eeabbc9ca3e3a4f03ff5ee"),
          ObjectId("611125cad17b0c71595298d4"),
          ObjectId("6113e8d69c6e77ad0fd50c08"),
          ObjectId("61279df3f544f6c2db48b92d"),
          ObjectId("612a3f061a1fcead8871e821")
        ],
        "_id": ObjectId("612a46c41a1fcead8871e83f"),
        "menu_id": ObjectId("611fe0b60734048a04812020")
      },
      {
        "permissions": [
          ObjectId("60eeabbc9ca3e3a4f03ff5ee"),
          ObjectId("611125cad17b0c71595298d4"),
          ObjectId("6113e8d69c6e77ad0fd50c08"),
          ObjectId("61279df3f544f6c2db48b92d"),
          ObjectId("612a3f061a1fcead8871e821")
        ],
        "_id": ObjectId("612a46c41a1fcead8871e840"),
        "menu_id": ObjectId("611fee7e0734048a04812022")
      },
      {
        "permissions": [
          ObjectId("60eeabbc9ca3e3a4f03ff5ee"),
          ObjectId("611125cad17b0c71595298d4"),
          ObjectId("6113e8d69c6e77ad0fd50c08"),
          ObjectId("61279df3f544f6c2db48b92d"),
          ObjectId("612a3f061a1fcead8871e821")
        ],
        "_id": ObjectId("612a46c41a1fcead8871e841"),
        "menu_id": ObjectId("6128c64f9bd697e83dbe47b1")
      },
      {
        "permissions": [
          ObjectId("60eeabbc9ca3e3a4f03ff5ee"),
          ObjectId("611125cad17b0c71595298d4"),
          ObjectId("6113e8d69c6e77ad0fd50c08"),
          ObjectId("61279df3f544f6c2db48b92d"),
          ObjectId("612a3f061a1fcead8871e821")
        ],
        "_id": ObjectId("612a46c41a1fcead8871e842"),
        "menu_id": ObjectId("6128c5e19bd697e83dbe47ac")
      }
    ],
    "createdAt": ISODate("2021-08-12T14:39:45.505+0000"),
    "updatedAt": ISODate("2021-08-28T14:23:00.040+0000"),
    "__v": NumberInt(10)
  }
]

what is want to get when i input menu_id = ObjectId("611fe0b60734048a04812020")

 {
    "permissions" : [
        ObjectId("60eeabbc9ca3e3a4f03ff5ee"), 
        ObjectId("611125cad17b0c71595298d4"), 
        ObjectId("6113e8d69c6e77ad0fd50c08"), 
        ObjectId("61279df3f544f6c2db48b92d"), 
        ObjectId("612a3f061a1fcead8871e821")
    ], 
    "_id" : ObjectId("612a46c41a1fcead8871e83f"), 
    "menu_id" : ObjectId("611fe0b60734048a04812020")
}, 

and this is my query

db.getCollection("tbl_roles").find(
 { "menu.menu_id": ObjectId("611fe0b60734048a04812020") }
 )

-- to no meaning word just for publish question In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available.

J.F.
  • 13,927
  • 9
  • 27
  • 65
Slack_ny
  • 39
  • 1
  • 4

1 Answers1

0

You can use $elemMatch which return only the first element matched in the array.

As explained into docs:

The $elemMatch operator limits the contents of an field from the query results to contain only the first element matching the $elemMatch condition.

Try this query:

db.collection.find({},
{
  "menu": {
    "$elemMatch": {
      "menu_id": ObjectId("611fe0b60734048a04812020")
    }
  }
})

Note that you can also add some matches into find object like this

Example here.

Also, if for any reason you need to get more than one result from the array, you can use an aggregate query like this one

J.F.
  • 13,927
  • 9
  • 27
  • 65