【问题标题】:Python: Filter data from mongodb based on name: something}Python:根据名称过滤来自 mongodb 的数据:something}
【发布时间】:2018-07-12 19:12:13
【问题描述】:

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

{
    "_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 作为输出。

【问题讨论】:

  • 只需要澄清一下,你是用python api还是mongo shell来查询的?
  • 我用的是pymongo所以是python api
  • 好的,很酷。我将在下面发布答案。让我知道它是否有效。

标签: python database mongodb


【解决方案1】:

希望这会有所帮助。

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)

【讨论】:

  • 这是您要找的吗?
猜你喜欢
  • 2019-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-03
  • 1970-01-01
  • 2023-03-15
  • 2019-04-02
  • 2012-09-22
相关资源
最近更新 更多