Mongodb Query example - How to search in array of object
This tutorials explains about Search an field in array of objects in Mongodb.
Let’s see an users
collection, Each document contains roles
key, contains array of objects or roles.
[
{
"_id": {
"$oid": "65165ba2bf2c71bd2475cec6"
},
"employee_id": 1,
"name": "john kumar",
"salary": 6000,
"roles": [
{
"name": "user",
"id": 1
},
{
"name": "admin",
"id": 2
},
{
"name": "sales",
"id": 3
},
{
"name": "hr",
"id": 4
}
]
},
{
"_id": {
"$oid": "65165ba2bf2c71bd2475cec7"
},
"employee_id": 2,
"name": "abc def",
"salary": 4000,
"roles": [
{
"name": "user",
"id": 1
},
{
"name": "admin",
"id": 2
}
]
},
{
"_id": {
"$oid": "65165ba2bf2c71bd2475cec8"
},
"employee_id": 3,
"name": "zyx kra",
"salary": 2000,
"roles": [
{
"name": "admin",
"id": 2
},
{
"name": "sales",
"id": 3
}
]
},
{
"_id": {
"$oid": "65165ba2bf2c71bd2475cec9"
},
"employee_id": 4,
"name": "test user",
"roles": [
{
"name": "user",
"id": 1
}
]
},
{
"_id": {
"$oid": "65165ba2bf2c71bd2475ceca"
},
"employee_id": 5,
"name": "test user",
"salary": "",
"roles": [
{
"name": "user",
"id": 1
}
]
},
{
"_id": {
"$oid": "65165ba2bf2c71bd2475cecb"
},
"employee_id": 5,
"name": "test user",
"salary": null,
"roles": [
{
"name": "user",
"id": 1
}
]
}
]
Let’s find Mongo queries to find nested array JSON based on filter condition
Mongodb Query find an object based on field matching in array of objects
- Find the all users whose roles contain name of
admin
.
Basically, Checks nested object array with matched condition, returns parent full document.
db.collection.find(queryobject)
returns all collections based on query criteria object.
[$elemMatch](https://www.mongodb.com/docs/manual/reference/operator/query/elemMatch/)
is an operator matches an object in array of objects for each document. It checks atleast one match and returns an document.
Syntax.
{ <field>: { $elemMatch: { <query1>, <query2>, ... } } }
It contains atleast one query option
db.users.find({ roles: { $elemMatch: { name: "admin" } } });
or;
db.users.find({ "roles.name": "admin" });
Output:
moviesdb>
[
{
_id: ObjectId("65165ba2bf2c71bd2475cec6"),
employee_id: 1,
name: 'john kumar',
salary: 6000,
roles: [
{ name: 'user', id: 1 },
{ name: 'admin', id: 2 },
{ name: 'sales', id: 3 },
{ name: 'hr', id: 4 }
]
},
{
_id: ObjectId("65165ba2bf2c71bd2475cec7"),
employee_id: 2,
name: 'abc def',
salary: 4000,
roles: [ { name: 'user', id: 1 }, { name: 'admin', id: 2 } ]
},
{
_id: ObjectId("65165ba2bf2c71bd2475cec8"),
employee_id: 3,
name: 'zyx kra',
salary: 2000,
roles: [ { name: 'admin', id: 2 }, { name: 'sales', id: 3 } ]
}
]
- Find the all users whose
roles
containsadmin
andsales
db.users.find({ roles: { $elemMatch: { name: "admin", name: "sales" } } });
Output:
[
{
_id: ObjectId("65165ba2bf2c71bd2475cec6"),
employee_id: 1,
name: 'john kumar',
salary: 6000,
roles: [
{ name: 'user', id: 1 },
{ name: 'admin', id: 2 },
{ name: 'sales', id: 3 },
{ name: 'hr', id: 4 }
]
},
{
_id: ObjectId("65165ba2bf2c71bd2475cec8"),
employee_id: 3,
name: 'zyx kra',
salary: 2000,
roles: [ { name: 'admin', id: 2 }, { name: 'sales', id: 3 } ]
}
]
- Find the specific user whose roles contain name of
admin
.
findOne
filters the records and returned matching first single record.
db.users.findOne({ $and: [{ employee_id: 1 }, { "roles.name": "admin" }] });
{
_id: ObjectId("65165ba2bf2c71bd2475cec6"),
employee_id: 1,
name: 'john kumar',
salary: 6000,
roles: [
{ name: 'user', id: 1 },
{ name: 'admin', id: 2 },
{ name: 'sales', id: 3 },
{ name: 'hr', id: 4 }
]
}