Query embedded documents and list embeded document with only matching values

21 views
Skip to first unread message

kart...@gmail.com

unread,
Jun 28, 2015, 8:06:27 AM6/28/15
to mongod...@googlegroups.com

Please can someone guide me as to how I can accomplish the below. I have a Banks document, that is embedded with child document "branches" which in turn has embedded documents "branchcontacts" . I am trying to query for branchContacts with whose designation is Manager.

What I get is the complete Bank document with both the child branchcontacts. How can I ensure the branchContacts with ONLY "Manager" designation are returned as part of the Bank document.

For eg: below when Bank document is returned I should not get branchcontacts for Lucy and Janet as their designation is Accountant.


{ 
"_id" : ObjectId("558e539941755bca3a966c10"), 
"bankCode" : "B001", 
"name" : "Progress Bank", 
"city" : "Paris", 
"branches" : [
    {
        "name" : "Standard-Gusto", 
        "address" : "address gusto", 
        "landLine1" : "343453453", 
        "mobile1" : "045865964", 
        "branchcontacts" : [
            {
                "contactName" : "Daniel", 
                "designation" : "Manager", 
                "emailID" : "dan...@prog.com", 
                "landLine1" : "3453453"
            }
            {
                "contactName" : "Lucy", 
                "designation" : "Accounant", 
                "emailID" : "lu...@prog.com", 
                "landLine1" : "7443456"
            }
        ]
    }
    {
        "name" : "Standard-Pronto", 
        "address" : "address pronto", 
        "landLine1" : "763453453", 
        "mobile1" : "46585964", 
        "branchContacts" : [
            {
                "contactName" : "Mary", 
                "designation" : "Manager", 
                "emailID" : "ma...@prog.com", 
                "landLine1" : "8453453"
            }
            {
                "contactName" : "Janet", 
                "designation" : "Accounant", 
                "emailID" : "ja...@prog.com", 
                "landLine1" : "9943456"
            }
        ]
    }
]
}



Akshay Kongalla

unread,
Jun 30, 2015, 8:21:00 AM6/30/15
to mongod...@googlegroups.com
I dont think Mongo provides this feature, You need to get the complete document and filter it using the language(eg.,Java Driver) you are using. And you can do one thing instead of fetching all the fields you can fetch only the Branches field using projections.

Asya Kamsky

unread,
Jun 30, 2015, 1:30:50 PM6/30/15
to mongodb-user
That's not correct - MongoDB does allow you to return just the managers.

You have to do it with aggregation framework by using $unwind:

db.bank.aggregate({$match:{"name" : "Progress Bank"}},{$unwind:"$branches"},{$unwind:"$branches.branchContacts"}, {$match:{"branches.branchContacts.designation":"Manager"}}).toArray()

[
{
"_id" : ObjectId("558e539941755bca3a966c10"),
"bankCode" : "B001",
"name" : "Progress Bank",
"city" : "Paris",
"branches" : {
"name" : "Standard-Gusto",
"address" : "address gusto",
"landLine1" : "343453453",
"mobile1" : "045865964",
"branchContacts" : {
"contactName" : "Daniel",
"designation" : "Manager",
"emailID" : "dan...@prog.com",
"landLine1" : "3453453"
}
}
},
{
"_id" : ObjectId("558e539941755bca3a966c10"),
"bankCode" : "B001",
"name" : "Progress Bank",
"city" : "Paris",
"branches" : {
"name" : "Standard-Pronto",
"address" : "address pronto",
"landLine1" : "763453453",
"mobile1" : "46585964",
"branchContacts" : {
"contactName" : "Mary",
"designation" : "Manager",
"emailID" : "ma...@prog.com",
"landLine1" : "8453453"
}
}
}
]

Note that your sample document had a typo - one branch had subdocument "branchContacts" and the other had "branchcontacts"

Asya




--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/1043aa4b-4d17-4106-838d-64dd4c0981a4%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages