SosaAlmighty
SosaAlmighty

Reputation: 369

Find only the documents which have two embedded documents

I'm using Mongodb to analysee a Nobel prizes dataset which documents look like these:

    > db.laureate.find().pretty().limit(1)
{
    "_id" : ObjectId("604bc8c847d640142f02b3b1"),
    "id" : "1",
    "firstname" : "Wilhelm Conrad",
    "surname" : "Röntgen",
    "born" : "1845-03-27",
    "died" : "1923-02-10",
    "bornCountry" : "Prussia (now Germany)",
    "bornCountryCode" : "DE",
    "bornCity" : "Lennep (now Remscheid)",
    "diedCountry" : "Germany",
    "diedCountryCode" : "DE",
    "diedCity" : "Munich",
    "gender" : "male",
    "prizes" : [
        {
            "year" : "1901",
            "category" : "physics",
            "share" : "1",
            "motivation" : "\"in recognition of the extraordinary services he has rendered by the discovery of the remarkable rays subsequently named after him\"",
            "affiliations" : [
                {
                    "name" : "Munich University",
                    "city" : "Munich",
                    "country" : "Germany"
                }
            ]
        }
    ]
}

As you see the column "prizes" has embedded documents and the query I am trying to do is finding only those laureates who won two prizes (which I already know to be Marie Curie and Linus Pauling) can you help me with that?

Thanks in advance!

Upvotes: 0

Views: 18

Answers (1)

Dori Lahav Waisberg
Dori Lahav Waisberg

Reputation: 980

The $size operator should work fine for this. You could read about it if you want in this link: https://docs.mongodb.com/manual/reference/operator/query/size/

Your new query:

db.laureate.find({prizes: {$size: 2}}).pretty().limit(1)

Upvotes: 1

Related Questions