Rivz
Rivz

Reputation: 83

Find in dictionary by value in Mongo

I have such structure in my Mongo db:

{'_id':'...', 
    'friends': 
        {'id1': {'name1':'value1', 'name2':'value2'},
        'id2': {'name1':'', 'name2':''},
         ...}
}

How can I find element(friend) in this dictionary(friends) by name1(value1)?

Upvotes: 8

Views: 18243

Answers (3)

Evgenii
Evgenii

Reputation: 3420

db.myCollection.find({"friends.id1.name1":"Sam"})

Upvotes: 16

Petrogad
Petrogad

Reputation: 4423

If I'm understanding your question correctly You can do this by:

  • db.collection.find({name:'value1'});

Here is a great resource to start learning mongo and various commands from it.

Interactive Mongo Tutorial

Upvotes: -2

dee-see
dee-see

Reputation: 24078

Is this what you mean?

db.dbname.find({name1:'value1'})

If value1 can be in any field, you can try:

db.dbname.find({$or:[{name1:'value1'},{name2:'value1'}]})

Upvotes: 0

Related Questions