3

I have researched throughly before posting this question but I could not find an accurate solution. I have the below structure

stname: "SC",
dob : "1985",
education {[
            {name : Lancaster,
             year : 2013},

            {name : Manchester, 
             year : 2001, 
             grad : 2004},

            {name : Gambia, 
             year : 2001, 
             grad : 2011}
         ]}

So I want to return only documents that have grad fields. So the last two documents should be returned.

I tried the following queries but to no avail

db.applicants.find({"education" : { $elemMatch : {"grad" : {$exists : true}}}}, {"name":1, "education.grad" : 1}).pretty()

returns only the first match as shown below,the first document is empty

{
    "_id" : ObjectId("574dd5fcbda73af19e361a3f"),
    "name" : "SC",
    "education" : [
        {

        },
        {
            "grad" : "2004"
        },
        {
            "grad" : "2011"
        }
    ]
}

Also the below query gives you similar results, that is an empty document where ever grad field is not available.

db.applicants.find({"education.grad" : { $exists : true}}, {"education.grad" : {$exists : true}, "education.grad" : 1, name : 1 , dob : 1}).pretty()
6
  • I've answer a similar question here: stackoverflow.com/questions/37510908/… Commented May 31, 2016 at 16:58
  • @felipsmartins, I think they are quite different, he was trying to query based on a field. Commented May 31, 2016 at 17:02
  • Oh, I see. I afraid you have to use aggregation. Commented May 31, 2016 at 17:41
  • @felipsmartins, see edit for the output of the first query. The empty document is the main problem. Without that everything will work fine. Commented May 31, 2016 at 18:28
  • Possible dupe of stackoverflow.com/questions/3985214/…. Answers for both single and multiple element cases can be found there. Commented May 31, 2016 at 21:50

1 Answer 1

2

UPDATE ANSWER:

db.applicants.aggregate({$unwind: "$education"}, 
{$match: {"education.grad":{$exists: true}}}, 
{$project: {"education.name": 1, "education.grad": 1, "_id": 0}})

This will return two records, no empty documents.

Sign up to request clarification or add additional context in comments.

4 Comments

this will give you the same results as my query above. You will have an empty document where ever grad is not available.
@fanbondi that is so strange. I tried to recreate your documents and write the same queries as you and I was not getting the same results that you posted. I think that there is something missing. To be clear, I was not getting an empty document either using my query or yours.
Hmm then this is interesting. What must be the problem then. I am using Mongo 3.2.5. Which version are you using. db.version() will give you the results.
I had entered a collection that was not a match to yours (the code provided at the beginning of the question has some formatting issues). See my new answer. I hope that this works! I am using 3.2.4.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.