dnit13
dnit13

Reputation: 2496

how to determine whether a field exists inside a value of a key?

I want to find all the documents in mongodb which have a "emotion1" key in "emotions" key. My database structure is like this

{
    "word" : "word1",
    "emotions" : 
     {
        "emotion1" : "0.25",
        "emotion2" : "0.25",
        "emotion3" : "0.35"
     }
}

I have tried,

db.collection.find({"emotions":{"emotion1":{"$exists":True}}})
db.collection.find({"emotions":{"emotion1":{"$exists":True}}})

However this works for "emotions", key like this,

db.collection.find({"emotions": {"$exists": True}})

Upvotes: 0

Views: 44

Answers (1)

Sede
Sede

Reputation: 61273

You need to use the dot notation.

db.collection.find( { "emotions.emotion1": { "$exists":True } } )

Upvotes: 1

Related Questions