Friday 20 February 2015

mongoDB : $elemMatch : Query Array


The $elemMatch operator matches documents in a collection that contain an array field with at least one element that matches all the specified query criteria.

Syntax
{ <field>: { $elemMatch: { <query1>, <query2>, ... } } }
 

Let’s experiment with employee collection.

> db.employee.find().pretty()
{
	"_id" : ObjectId("60bcd0dd8251689f64fb3eee"),
	"id" : 1,
	"firstName" : "Joel",
	"lastName" : "chelli"
}
{
	"_id" : ObjectId("60bcd0dd8251689f64fb3eef"),
	"id" : 2,
	"firstName" : "Ananad",
	"lastName" : "Bandaru"
}
{
	"_id" : ObjectId("60bcd0dd8251689f64fb3ef0"),
	"id" : 3,
	"firstName" : "Gopi",
	"lastName" : "Battu",
	"projects" : [
		{
			"projectId" : "ind_cpcs",
			"name" : "cpcs",
			"description" : "cabin pressure control system"
		},
		{
			"projectId" : "ind_veh_tracker",
			"name" : "vehicle tracker",
			"description" : "Track the vehicle"
		}
	]
}
{
	"_id" : ObjectId("60bcd0dd8251689f64fb3ef1"),
	"id" : 4,
	"firstName" : "Ritwik",
	"lastName" : "Mohenthy",
	"hobbies" : [
		"cooking",
		"trekking",
		"listening to music"
	]
}
{
	"_id" : ObjectId("60bcf6218251689f64fb3ef2"),
	"id" : 5,
	"firstName" : "Sailja",
	"lastName" : "PTR",
	"projects" : [
		{
			"projectId" : "ind_cpcs",
			"name" : "cpcs",
			"description" : "cabin pressure control system"
		}
	],
	"hobbies" : [
		"trekking",
		"singing"
	]
}

Get all the employees whose project name is cpcs.

> db.employee.find({"projects" : {$elemMatch : {"name" : {$eq : "cpcs"}}}}).pretty()
{
    "_id" : ObjectId("60bcd0dd8251689f64fb3ef0"),
    "id" : 3,
    "firstName" : "Gopi",
    "lastName" : "Battu",
    "projects" : [
        {
            "projectId" : "ind_cpcs",
            "name" : "cpcs",
            "description" : "cabin pressure control system"
        },
        {
            "projectId" : "ind_veh_tracker",
            "name" : "vehicle tracker",
            "description" : "Track the vehicle"
        }
    ]
}
{
    "_id" : ObjectId("60bcf6218251689f64fb3ef2"),
    "id" : 5,
    "firstName" : "Sailja",
    "lastName" : "PTR",
    "projects" : [
        {
            "projectId" : "ind_cpcs",
            "name" : "cpcs",
            "description" : "cabin pressure control system"
        }
    ],
    "hobbies" : [
        "trekking",
        "singing"
    ]
}

You can get the same result with following query also.

db.employee.find({"projects.name" : "cpcs" }).pretty()

> db.employee.find({"projects.name" : "cpcs" }).pretty()
{
    "_id" : ObjectId("60bcd0dd8251689f64fb3ef0"),
    "id" : 3,
    "firstName" : "Gopi",
    "lastName" : "Battu",
    "projects" : [
        {
            "projectId" : "ind_cpcs",
            "name" : "cpcs",
            "description" : "cabin pressure control system"
        },
        {
            "projectId" : "ind_veh_tracker",
            "name" : "vehicle tracker",
            "description" : "Track the vehicle"
        }
    ]
}
{
    "_id" : ObjectId("60bcf6218251689f64fb3ef2"),
    "id" : 5,
    "firstName" : "Sailja",
    "lastName" : "PTR",
    "projects" : [
        {
            "projectId" : "ind_cpcs",
            "name" : "cpcs",
            "description" : "cabin pressure control system"
        }
    ],
    "hobbies" : [
        "trekking",
        "singing"
    ]
}



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment