Querying "greater than" in an array

1,774 views
Skip to first unread message

jgoetzfr

unread,
Mar 14, 2012, 5:27:59 PM3/14/12
to mongodb-user
Hi everyone,

I've developped my first web-application with the powerfull mongodb
but i'm stuck in a final problem.
In my db model, i've got an array listing multiple dates and I'd like
to create a query looking for entries containing one or more dates in
the dates array greater than the on I specified in the querie.

But apparently, mongodb does not allow the "gt" filter on arrays.

Does my problem sounds clear ?
If anyone got an idea...

Many thanks in advance,
Julien

Tim Rieder

unread,
Mar 14, 2012, 5:50:44 PM3/14/12
to mongod...@googlegroups.com
Given:

> db.a.find()
{ "_id" : ObjectId("4f61110ea472a6f00d6d44c8"), "n" : 1, "x" : [ 1, 2, 3, 4, 5 ] }
{ "_id" : ObjectId("4f611117a472a6f00d6d44c9"), "n" : 2, "x" : [ 2, 1, 3, 5, 4 ] }
{ "_id" : ObjectId("4f611213a472a6f00d6d44ca"), "n" : 1, "x" : [ 1, 2 ] }

Use elemMatch:

> db.a.find({ x:{"$elemMatch":{$gt:3}} })
{ "_id" : ObjectId("4f61110ea472a6f00d6d44c8"), "n" : 1, "x" : [ 1, 2, 3, 4, 5 ] }
{ "_id" : ObjectId("4f611117a472a6f00d6d44c9"), "n" : 2, "x" : [ 2, 1, 3, 5, 4 ] }

Dates should work as well.


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


jgoetzfr

unread,
Mar 15, 2012, 5:41:39 AM3/15/12
to mongodb-user
Thanks Tim ! I'll try this today.

Julien

Mike

unread,
Mar 22, 2012, 4:59:40 PM3/22/12
to mongodb-user
Hey Tim,

How would this work if your array was an embedded document and you
querying by an element in that doc.

For example

{ "_id" : ObjectId("4f61110ea472a6f00d6d44c8"), "n" : 1, "x" : [ {num:
2}, {num:3},
{num:4}, {num:5} ] }
{ "_id" : ObjectId("4f611117a472a6f00d6d44c9"), "n" : 2, "x" : [ {num:
2}, {num:1}, {num:3},
{num:5}, {num:4} ] }

Thanks,

Mike

Marc

unread,
Mar 22, 2012, 5:45:31 PM3/22/12
to mongodb-user
Given the following collection:

> db.a.find()
{ "_id" : 1, "n" : 1, "x" : [ { "num" : 1 }, { "num" : 2 }, { "num" :
3 } ] }
{ "_id" : 2, "n" : 2, "x" : [ { "num" : 2 }, { "num" : 3 }, { "num" :
5 } ] }

You can find the document whose "x" array contains an embedded
document containing a value of "num" greater than 4 by using dot
notation:

> db.a.find({"x.num":{$gt:4}})
{ "_id" : 2, "n" : 2, "x" : [ { "num" : 2 }, { "num" : 3 }, { "num" :
5 } ] }

The documentation on querying embedded documents with Mongo may be
found here:
http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29

The above link also contains a section on how the $elemMatch operator
may be used in a similar situation.

Documentation on $gt and other operators can be found in the "Advanced
Queries" documentation:
http://www.mongodb.org/display/DOCS/Advanced+Queries

Hopefully the above will help you to make your desired queries. If
you have any additional questions, the Mongo Community is here to help!
Reply all
Reply to author
Forward
0 new messages