Harry
Harry

Reputation: 54939

Mongodb query from any array position

My schema looks like this:

{
  _id: "myid"
  user: [{"name":"Bob"}, {"name":"Jenny"}]
},
{
  _id: "myid2"
  user: [{"name":"John"}, {"name":"Jenny"}]
},
{
  _id: "myid3"
  user: [{"name":"John"}, {"name":"Bob"}]
}

I want to find all the documents Bob is an user of. He could be any any position in the array. Could this be done?

Thanks.

Upvotes: 0

Views: 73

Answers (1)

Abhishek Kumar
Abhishek Kumar

Reputation: 3356

1) db.test.find({'user.name': 'Bob'})

2) db.test.find({'user' : { $elemMatch : {'name' : 'Bob'}}})

Upvotes: 4

Related Questions