Skip to main content
 首页 » 编程设计

python之使用 "$gte"和 "$lte"时 Pymongo 返回错误

2025年05月04日19think

我正在尝试从 mongo(使用 pymongo)中进行选择,查询纪元时间戳。 我的数据库中的示例是(为简洁起见进行了删减):

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

以及在代码和 python 控制台中的查询:

<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}}) 

分别是 2012 年 12 月 13 日星期四 09:30:00 GMT 和 2012 年 12 月 13 日星期四 16:00:00 GMT。

应该是说,给我所有推文gte this int 和lte this other int。但是,它返回一切 - 它似乎根本没有限制值。

例如,返回此条目,时间戳为:1355325948 即:Wed, 12 Dec 2012 15:25:48 GMT

另外,我的理解是带有参数列表的 find(...) 是一个隐式的 AND。

太好了!

请您参考如下方法:

查询对象中不能有两个同名的键。您必须像这样将它们组合成一个值:

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