草庐IT

python - MongoDB/PyMongo : BadValue Unsupported projection option when trying to query all dates after

coder 2023-11-04 原文

我构建了一个将推文存储到 MongoDB 中的 Twitter 抓取工具。现在我正在尝试使用 PyMongo 查询数据。

在我的 MongoDB 中存储的数据:

{
    "_id": {
        "$oid": "5555dc0e50f808afe0da52fe"
    },
    "text": "Lorem Ipsum...",
    "created_at": {
        "$date": "2015-05-15T10:55:16.000Z"
    },
}

以下工作非常好(但获取每条推文):

dikt1 = {}
tweets_iterator = coll.find({},{ "text": 1, "user.screen_name":1 ,created_at': 1} )

for tweet in tweets_iterator:
        dikt1[tweet['text']] = tweet['created_at']

然而,当像这样尝试查询特定日期之后的所有日期时:

date1 = datetime.utcnow()-timedelta(days=30)
dikt1 = {}
tweets_iterator = coll.find({},{ "text": 1, "user.screen_name":1 , 'created_at': {'$gt': date1}} )
for tweet in tweets_iterator:
        dikt1[tweet['text']] = tweet['created_at']

最后一行“for tweet in tweets_iterator:”报错:

OperationFailure: database error: Can't canonicalize query: BadValue Unsupported projection option: created_at: { $gt: new Date(1434480147418) }

有什么建议吗?

谢谢!

最佳答案

如果要应用过滤器,请使用 find() 的第一个位置参数:

tweets_iterator = coll.find({'created_at': {'$gt': date1}}, {"text": 1, "user.screen_name": 1, 'created_at': 1})

关于python - MongoDB/PyMongo : BadValue Unsupported projection option when trying to query all dates after,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31169416/

有关python - MongoDB/PyMongo : BadValue Unsupported projection option when trying to query all dates after的更多相关文章

随机推荐