Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Sup, good folks of the internet.

Does anyone know how to make nested queries for mongodb? This is probably best explained by an example. To retrieve specific fields, I can use the :fields option to retrieve that field (e.g. suppose it is called "useful_field"):

    collection.find({},{:fields => {"useful_field" => 1}})

But suppose that useful_field itself contains an array of many further fields, i.e

    useful_field = [{"value_I_want"=>"useful","value_I_dont_want"=>"not_useful"}]

My aim is to select "value_I_want". Any thoughts?

Here is a specific entry that I am trying to deal with (a reply to a tweet):

    { "_id" : ObjectId("51b6f71b0364718d71e4bca5"), 
    "annotations" : { }, 
    "resultType" : "Tweet", 
    "score" : 1, 
    "groupName" : "TweetsWithConversation",
    "results" : [
            {   
                "kind" : "Tweet",   
                "score" : 1,    
                "annotations" : {   "ConversationRole" : "Ancestor" },
              "value" : {   "created_at" : "Fri Jun 07 19:47:51 +0000 2013",    
                          "id" : NumberLong("343091955196104704"),  
                          "id_str" : "343091955196104704",  
                          "text" : "THIS_IS_WHAT_I_WANT",
                       etc. etc. (Apologies for the odd formatting)

I'm trying to use a method of the form that will let me do something like this:

    db.collection.find({},{:fields { some_way_of_selecting(THIS_IS_WHAT_I_WANT)})

(I'm querying as part of a ruby script)

Otherwise, I'll have to go back into the dark world of regex. No-one wants that.

share|improve this question
Just use the projection like this: db.coll.find({}, {"results.value.text":1} ) – Asya Kamsky Jun 22 at 3:59

2 Answers

up vote 0 down vote accepted

Try the following

db.collection.find({},{"useful_field.value_I_want": 1})

Maybe try this:

db.collection.find({"resultType" : "Tweet"}, {"results" : {$elemMatch : {"value.text" : "THIS_IS_WHAT_I_WANT"}}})
share|improve this answer
Thanks for answering! Unfortunately I wasn't able to get it to work with the method above. It's frustrating that this isn't mentioned in the documentation about the options hash for the find method. – Sam Jun 21 at 17:21
can you post the actual document? maybe I can be more help if I can see it. also, how are you querying? command line? – seeshing Jun 21 at 17:41
Thanks a lot man, I think the reason it wasn't working was that it was the square brackets for the results array - the $elemMatch is the way to go! – Sam yesterday

What you are trying to do is called "projection" - it's specifying what fields you want returned in the second argument to find.

In your case you simply want:

db.collection.find({}, {"results.value.text":1} ) 
share|improve this answer
Thanks! It was much easier to find documentation about it once you mentioned it was called "projection". For some reason, the [ inside results seems to stop the "results.value.text" selector method working for me. – Sam yesterday
are you sure? I just tested it and it works fine for me. can you post (in comment or by editing the original question) the exact syntax you are trying and maybe we can spot what's not quite right. – Asya Kamsky yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.