如果我使用变量作为值,则在python中进行Mongo查询 [英] Mongo query in python if I use variable as value
本文介绍了如果我使用变量作为值,则在python中进行Mongo查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正尝试使用以下查询从mongo集合中查找文档. db.collection_name.find({"id" : Id})
其中id是变量正在作为输入.但这是行不通的.如果我用硬编码这样的值db.collection_name.find({"id" : "1a2b"})
,它将起作用. "id"是字符串类型,正在使用pymongo访问mongo DB.
Am trying to find documents from the mongo collection using the following query. db.collection_name.find({"id" : Id})
where Id is the variable am getting as input. But it doesn't work. If I hard code the value like this db.collection_name.find({"id" : "1a2b"})
it works. "id" is of string type and am using pymongo to access mongo DB.
代码:
client = MongoClient("localhost:27017")
db = client['sample_database']
Id = raw_input("enter id")
cursor = db.collection_name.find({"id" : Id})
推荐答案
尝试str();
Id = str(raw_input("enter id"))
cursor = db.collection_name.find({"id" : Id})
这篇关于如果我使用变量作为值,则在python中进行Mongo查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文