mr-sk
mr-sk

Reputation: 13417

Pymongo returning incorrectly when using "$gte" and "$lte"

I'm attempting to select from mongo (using pymongo), querying against epoch timestamps. A sample from my database is (pruned for brevity):

{
    "_id": "",
    "tweet": {
        "iso_language_code": "en",
        "to_user_name": null,
        "timestamp": 1355325948,
        "created_at": "Wed, 12 Dec 2012 15:25:48 +0000"
    }
}

And the query, in code and within python console:

<console>
db.tweets.find({
    "tweet.timestamp":{'$gte':1355391000},
    "tweet.timestamp":{'$lte':1355414400}
})

<code>
cursor = db.tweets.find({"tweet.timestamp":{'$gte':startEpoch},
                         "tweet.timestamp":{'$lte':endEpoch}})

Which are Thu, 13 Dec 2012 09:30:00 GMT and Thu, 13 Dec 2012 16:00:00 GMT respectively.

It should be saying, get me all tweets gte this int and lte this other int. However, it returns EVERYTHING - it doesn't seem to be limiting the values at all.

For example, this entry is returned, and the timestamp is: 1355325948 which is: Wed, 12 Dec 2012 15:25:48 GMT

Also, my understanding that find(...) with a list of params is an implicit AND.

TIA SO!

Upvotes: 5

Views: 13454

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 312005

You can't have two keys in your query object with the same name. You have to combine them into a single value like this:

cursor = db.tweets.find({"tweet.timestamp":{'$gte':startEpoch, '$lte':endEpoch}})

Upvotes: 15

Related Questions