Dot
notation is used to query nested documents.
Let’s
say I had below data in person collection.
db.person.insert(
[
{
"name" : "Hari Krishna",
"address" :{
"home" : {
"city" : "ongole",
"state" : "Andhra Pradesh",
"country" : "India"
},
"office" : {
"city" : "Bangalore",
"state" : "Karnataka",
"country" : "India"
}
}
},
{
"name" : "PTR",
"address" :{
"home" : {
"city" : "Gudivada",
"state" : "Andhra Pradesh",
"country" : "India"
},
"office" : {
"city" : "Hyderabad",
"state" : "Andhra Pradesh",
"country" : "India"
}
}
}
]
)
> db.person.find().pretty()
{
"_id" : ObjectId("54ba5928f1c8f2149c000743"),
"name" : "Hari Krishna",
"address" : {
"home" : {
"city" : "ongole",
"state" : "Andhra Pradesh",
"country" : "India"
},
"office" : {
"city" : "Bangalore",
"state" : "Karnataka",
"country" : "India"
}
}
}
{
"_id" : ObjectId("54ba5928f1c8f2149c000744"),
"name" : "PTR",
"address" : {
"home" : {
"city" : "Gudivada",
"state" : "Andhra Pradesh",
"country" : "India"
},
"office" : {
"city" : "Hyderabad",
"state" : "Andhra Pradesh",
"country" : "India"
}
}
}
1. Get document
where home city is “ongole”.
> db.person.find({"address.home.city" : "ongole"}).pretty()
{
"_id" : ObjectId("54ba5928f1c8f2149c000743"),
"name" : "Hari Krishna",
"address" : {
"home" : {
"city" : "ongole",
"state" : "Andhra Pradesh",
"country" : "India"
},
"office" : {
"city" : "Bangalore",
"state" : "Karnataka",
"country" : "India"
}
}
}
As
you see, . notation is used to get the home city like “address.home.city”.
No comments:
Post a Comment