Mongodb: How to select objects in which an array contains only a specific field?

I have two objects:

{
        "_id" : ObjectId("54be5f5528c13bfc3409e8c2"),
        "name" : "Antonio",
        "lastname" : "de Cabezón",
        "by" : 1510,
        "dy" : 1566,
        "country" : "spain",
        "genre" : [
                "classical",
                "baroque"
        ]
}
{
        "_id" : ObjectId("54be5f5528c13bfc3409e8c1"),
        "name" : "Guillaume-Antoine",
        "lastname" : "Calvière",
        "by" : 1695,
        "dy" : 1755,
        "country" : "france",
        "genre" : [
                "baroque"
        ]
}

When I do db.currentdb.find ({genre: 'baroque'}), it also returns the first object to me.

I would like to get only an object in which the genre is only "baroque". What would be the right way to do this?

+4
source share
1 answer

You can try

db.currentdb.find({genre: ['baroque']})

Also, check out the documentation:

https://docs.mongodb.org/manual/reference/method/db.collection.find/

+7
source

All Articles