2014년 12월 7일 일요일

How to find a subdocument in an array?

I am new to Mongo DB and need to use it for a class project. How do you write a query to find a sub document in an array? I tried the following but it brings back the full document.
 
{ 'statuses.id_str': "512383762911744000" }
 
 I attached a sample DB of what I am testing with.




You are correctly searching on a subdocument of an array, but a search (find) always matches the whole document that has the matching fields/subfields.

If you don't want the entire document returned, then you need to make use of the second argument to find and create a projection expressionappropriate to what you want to get back.



Thank you for your help Asya!
 
I tried the following two queries but they give me just the document ID not the field text. Do these statements look correct? Would you have an example that I could try?



Attached is a picture from MongoVue on what the data looks like in the document.




There is no field text to return - there is a subdocument with field text...

Try projecting "statuses.text" instead and see if that works better for you...


댓글 없음:

댓글 쓰기