【发布时间】:2016-03-14 10:34:48
【问题描述】:
我如何在 MongoDB 中创建 find(),使用 find 为 >= 某个值,但该值是数字字符串?
如果我运行以下行(在 MongoDB 数据库中搜索高于 1 的模式):
cursor = db.foo.find({"mode": {"$gt": 1}})
这只有在 MongoDB 中的数据采用以下格式时才有效:
data = {"mode":3}
但我需要将find() 与这些数据一起使用:
data = {"mode":'3'} # as string
我该怎么做?
这是我的例子:
from pymongo import MongoClient
client = MongoClient()
db = client.test
db.foo.drop()
data = {"mode":3} # Works because this is a numeric
data = {"mode":'3'} # Won't work!!!!!!!!!! But my database contains only numeric strings...how can use like this?
db.foo.insert_one(data)
print(db.foo.count())
cursor = db.foo.find({"mode": {"$gt": 1}})
for document in cursor:
print(document)
【问题讨论】:
-
数字字符串背后的原因是什么?您不能将其存储为数字并在应用程序级别转换为字符串吗?
-
可惜数据库已经是这个样子了……改不了了。
-
根据您的收藏有多大,我建议您更新您的文档并使其具有适当的类型。