Mongo Query

2 views
Skip to first unread message

urvik

unread,
Oct 9, 2010, 6:49:20 AM10/9/10
to mongo-scala-driver
Hi All,

I added few data into Mongo Collection. Three data samples are as
below.

{ "type" : "ABC", "ABC" : [{ "Test" : "Test1 details", "counter" :
1 }, { "Test" : "Test2 details", "counter" : 0 }]}


{ "type" : "XYZ", "XYZ" : [ { "Description" : "Description1 here",
"size" : 70, "counter" : 22 }, { "Description" : "Description2 here",
"size" : 55, "counter" : 10}]}


{ "type" : "XYZ" , "XYZ" : [ { "Description" : "Description3 here" ,
"size" : 36 , "counter" : 13} , { "Description" : "Description4 here",
"size" : 24 , "counter" : 0}]}


I want to find Description and counter values where type = XYZ and
xyz.size is less than 30.I also want to get results sorted by counter
value.

I wrote query db.temp.find({"type" : "XYZ","XYZ.size":{"$lt":30}},
{"XYZ.Description" : 1, "XYZ.counter" : 1})

But got result as below:

{ "_id" : ObjectId("4cb04b621637070000005164"), "XYZ" : [
{
"Description" : "Description3 here",
"counter" : 13
},
{
"Description" : "Description4 here",
"counter" : 0
}
] }

"Description3 here" record should not come as it's size is 36(not less
than 30).

I am new to Mongo. Can some one guide how to resolve this using one
query?

Thanks in advance.

Urvik

Alexander Azarov

unread,
Oct 9, 2010, 6:59:14 AM10/9/10
to mongo-scala-driver
Mongo finds documents, i.e. the top-level documents in a collection.

Thus, your query asks Mongo to find a document where at least one of
the sub-documents must match your criteria (less than 30).

Urvik Patel

unread,
Oct 9, 2010, 8:38:03 AM10/9/10
to mongod...@googlegroups.com
Yes, I want to find document where at least one of the sub-documents match some criteria.
 
Is it possible? If yes, then can you guide me how?

Regards,

Urvik


Alexander Azarov

unread,
Oct 9, 2010, 1:37:56 PM10/9/10
to mongod...@googlegroups.com
09.10.2010, в 16:38, Urvik Patel wrote:

Yes, I want to find document where at least one of the sub-documents match some criteria.
 
That's exactly why you are getting "Description3" in your example.

Urvik Patel

unread,
Oct 10, 2010, 1:28:33 AM10/10/10
to mongod...@googlegroups.com
Thanks Alexander. So we cannot just get only sub-document in result. Mongo will return whole document. 

Regards,

Urvik


2010/10/9 Alexander Azarov <aza...@aha.ru>
Reply all
Reply to author
Forward
0 new messages