【问题标题】:PyMongo get to find json object based on field valuePyMongo 根据字段值查找 json 对象
【发布时间】:2021-01-19 06:56:20
【问题描述】:

我用这样的集合创建了一个 MongoDB:

{
    "FR" : {...},
    "EN": {...}
}

我对 MongoDB/PyMongoDB 世界很陌生,所以我想知道是否有办法根据键(FR 或 EN)获取数据?

我试过这个:db.collection.find_one({'EN'}),但没用。

干杯,

【问题讨论】:

  • 这能回答你的问题吗? Pymongo using $exists
  • 不确定如何使用$exists 关键字。但我有这个表达:db.collection.find({'EN': {'$exists': True}}) 我获得了所有的集合(FR 和 EN)

标签: python database mongodb nosql pymongo


【解决方案1】:

find() 使用filterprojection 作为first two parameters。过滤器确定返回哪些文档,投影确定返回哪些字段。

所以要获取你有兴趣使用的数据:

for doc in db.collection.find({}, {'EN': 1}):
    print(doc.get('EN'))

【讨论】:

  • 不使用循环,我可以直接使用index = 0,因为item的结果只有一个值。例如:db.collection.find({}, {'EN': 1})[0]
  • 循环存在于 records 而不是 field 。如果您知道您只需要一条记录,请使用find_one()
猜你喜欢
  • 1970-01-01
  • 2012-08-15
  • 1970-01-01
  • 2017-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
相关资源
最近更新 更多