TIMEX
TIMEX

Reputation: 271594

How do I do greater than/less than using MongoDB?

I'm using the pymongo driver.

Can someone take a look at pymongo and tell me how to do greater than? I"m used to doing : for everything.

Upvotes: 35

Views: 62249

Answers (3)

Xavier Barbosa
Xavier Barbosa

Reputation: 3947

Have you seen the doc ? Take from the manual :

>>> d = datetime.datetime(2009, 11, 12, 12)
>>> for post in posts.find({"date": {"$lt": d}}).sort("author"):
...   post
...
{u'date': datetime.datetime(2009, 11, 10, 10, 45), u'text': u'and pretty easy too!', u'_id': ObjectId('...'), u'author': u'Eliot', u'title': u'MongoDB is fun'}
{u'date': datetime.datetime(2009, 11, 12, 11, 14), u'text': u'Another post!', u'_id': ObjectId('...'), u'author': u'Mike', u'tags': [u'bulk', u'insert']}

Upvotes: 55

trickwallett
trickwallett

Reputation: 2468

gt & lt are the operators. See http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart

Upvotes: 7

Sai Venkat
Sai Venkat

Reputation: 1248

If you wanna query and find docs which have a field greater than something you can

users.find({"age": {"$gt": 20}})

Check out the advanced query section of Mongodb for more reference.

--Sai

Upvotes: 31

Related Questions