gpt4 book ai didi

python - 使用pymongo在mongodb中按时间范围查询

转载 作者:可可西里 更新时间:2023-11-01 09:31:10 27 4
gpt4 key购买 nike

需要对数据库 mongodb 执行查询,按时间范围生成一组结果。我正在使用 pymongo。我的查询如下:

query = {"$and": [
{"id_node": id_node}
{"port": port},
{"datetime": {"$gte": self.hourBegin}}
{"datetime": {"$lte": self.now}}
]}
listResults = db.mycollection.find (query)

我也试过这种方式:

query = {"id_node": int(id_node)
"port": port,
"datetime": {"$gte": self.hourBegin, "$lte": self.now}}
listResults = db.mycollection.find (query)

但结果总是空的。直接在mongodb中测试但是结果是空的。我确定数据库中存在我正在研究的时间范围内的数据。

可能的结果列表:

{ "_id" : ObjectId("543618c6e7b9914c35266128"), "lab" : "2", "port" : "A1", "id_node" : 1, "datetime" : ISODate("2014-09-26T18:28:04Z"), "valor" : "22.00", "sensor" : "2" }
{ "_id" : ObjectId("543618c6e7b9914c35266129"), "lab" : "2", "port" : "A0", "id_node" : 1, "datetime" : ISODate("2014-09-26T18:28:04Z"), "valor" : "0", "sensor" : "1" }
{ "_id" : ObjectId("543618c6e7b9914c3526612c"), "lab" : "2", "port" : "A1", "id_node" : 1, "datetime" : ISODate("2014-09-26T18:28:06Z"), "valor" : "22.00", "sensor" : "2" }
{ "_id" : ObjectId("543618c6e7b9914c3526612d"), "lab" : "2", "port" : "A0", "id_node" : 1, "datetime" : ISODate("2014-09-26T18:28:06Z"), "valor" : "0", "sensor" : "1" }
{ "_id" : ObjectId("543618c7e7b9914c35266130"), "lab" : "2", "port" : "A1", "id_node" : 1, "datetime" : ISODate("2014-09-26T18:28:08Z"), "valor" : "22.00", "sensor" : "2" }
{ "_id" : ObjectId("543618c7e7b9914c35266131"), "lab" : "2", "port" : "A0", "id_node" : 1, "datetime" : ISODate("2014-09-26T18:28:08Z"), "valor" : "0", "sensor" : "1" }
{ "_id" : ObjectId("543618c7e7b9914c35266134"), "lab" : "2", "port" : "A1", "id_node" : 1, "datetime" : ISODate("2014-09-26T18:28:10Z"), "valor" : "22.00", "sensor" : "2" }
{ "_id" : ObjectId("543618c7e7b9914c35266135"), "lab" : "2", "port" : "A0", "id_node" : 1, "datetime" : ISODate("2014-09-26T18:28:10Z"), "valor" : "0", "sensor" : "1" }
{ "_id" : ObjectId("543618c7e7b9914c35266138"), "lab" : "2", "port" : "A1", "id_node" : 1, "datetime" : ISODate("2014-09-26T18:28:12Z"), "valor" : "22.00", "sensor" : "2" }
{ "_id" : ObjectId("543618c7e7b9914c35266139"), "lab" : "2", "port" : "A0", "id_node" : 1, "datetime" : ISODate("2014-09-26T18:28:12Z"), "valor" : "0", "sensor" : "1" }

最佳答案

我访问http://www.querymongo.com/ ,然后像在 MySQL 中一样创建一个查询,然后必须返回一个 mongodb 查询。像这样:

#MySQL Query
SELECT * FROM inoshare WHERE id_nome = 1 AND port = 1 AND datetime > 'hourBegin' AND datetime <= 'NOW()';

返回:

db.inoshare.find({
"id_nome": 1,
"port": 1,
"datetime": {
"$gt": "hourBegin",
"$lte": "NOW()"
}
});

然后将“hourBegin”和“NOW ()”替换为变量。谢谢大家。

关于python - 使用pymongo在mongodb中按时间范围查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26352367/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com