Python:根据name:something}从mongodb筛选数据
因此,我的mongodb中有以下数据:Python:根据name:something}从mongodb筛选数据,python,database,mongodb,Python,Database,Mongodb,因此,我的mongodb中有以下数据: { "_id": { "$oid": "5b479eca54159200044ca729" }, "name": "note1", "content": 118 } { "_id": { "$oid": "5b479eca54159200044ca729" }, "name": "note2", "content": 122 } { "_id": {
{
"_id": {
"$oid": "5b479eca54159200044ca729"
},
"name": "note1",
"content": 118
}
{
"_id": {
"$oid": "5b479eca54159200044ca729"
},
"name": "note2",
"content": 122
}
{
"_id": {
"$oid": "5b479eca54159200044ca729"
},
"name": "note3",
"content": 920
}
例如,如何获取note3的内容?或者如果用户想要获取note2内容
因此,为了更好地解释它:
如果我输入控制台note2,我应该得到122,如果我输入note3,我应该得到920作为输出。希望这有帮助
from pymongo import MongoClient
# I made an assumption your db and collection were called 'test'
client = MongoClient()
db = client.test
collection = db.test
find = "note1"
curs = collection.find({"name":find}, {"content":True, "_id":False})
for item in curs:
print(item)
只需要澄清一下,您是在使用python api还是mongo shell进行查询?我使用的是pymongo,所以它是python api OK,酷。我将在下面发布一个答案。让我知道它是否有效。这就是你要找的吗?