Python:根据name:something}从mongodb过滤数据

2024-09-24 22:27:26 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我在我的mongodb中有以下数据:

{
    "_id": {
        "$oid": "5b479eca54159200044ca729"
    },
    "name": "note1",
    "content": 118
}
{
    "_id": {
        "$oid": "5b479eca54159200044ca729"
    },
    "name": "note2",
    "content": 122
}
{
    "_id": {
        "$oid": "5b479eca54159200044ca729"
    },
    "name": "note3",
    "content": 920
}

例如,如何获取note3的内容?或者如果用户想要获取note2内容?你知道吗

所以,为了更好地解释:

如果我输入console note2,我应该得到122,如果我输入note3,我应该得到920作为输出。你知道吗


Tags: 数据用户nameid内容mongodbcontentconsole
1条回答
网友
1楼 · 发布于 2024-09-24 22:27:26

希望这有帮助。你知道吗

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)

相关问题 更多 >