2

I have a simple structured document like this:

"people" : [
    {
        "id" : "6241863",
        "amount" : 5
    }
],

People can contain more than one element. I've managed to get this to work:

db.village.findOne({"people": {"$in": [{"id": "6241863", "amount": 5}]}})

But I want to ignore amount and search for any document containing people with id 6241863 and any amount.

2
  • you can't retrieve part of array of single document in find query. you must do it in client side Commented Jul 10, 2012 at 14:50
  • dude how did u save the data like that , i have a shema like this product_video:[{thum_src:String,video_name:String,video_path:String}] and i dont know how to save the data Commented Dec 7, 2012 at 0:54

2 Answers 2

5

According to the advanced query documentation, you can mix array value queries with dot notation for reaching into objects. Here's an example using your schema:

$ mongo
MongoDB shell version: 2.1.0
connecting to: test
> db.users.save({_id: 1, friends: [{id: 2, name: 'bob'}]})
> db.users.find({friends: {id: 2, name: 'bob'}})
{ "_id" : 1, "friends" : [ { "id" : 2, "name" : "bob" } ] }

> db.users.find({'friends.id': 2})
{ "_id" : 1, "friends" : [ { "id" : 2, "name" : "bob" } ] }

> db.users.find({'friends.name': 'bob'})
{ "_id" : 1, "friends" : [ { "id" : 2, "name" : "bob" } ] }

> db.users.find({'friends.name': 'ted'})
> 
Sign up to request clarification or add additional context in comments.

1 Comment

How would you go about doing this if there was an object above users, such as roles that contained users and admins, and you still wanted to search just users?
0

Try with that

db.village.findOne({"people" : { "$in" : [{ "id" : "6241863" , "amount" : { "$ne" : null } }]}})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.