【问题标题】:How to check if string is in pymongo.collection.collection object如何检查字符串是否在 pymongo.collection.collection 对象中
【发布时间】:2021-11-20 23:07:33
【问题描述】:

我正在寻找解决方案,但找不到任何实用的解决方案

假设我们有一个 pymongo collection 字符串,我们正在其中搜索名为 "tk_dd_id" 的字符串。

collection =   Collection(Database(MongoClient(host=['123234'], document_class=dict, tz_aware=False, connect=True, ssl=True, replicaset='mongomongo'), 'health_cards'), 'tk_dd_id')

我试过了

if "tk_dd_id" in collection:
        print("Found!")

'Collection' 对象不可迭代

类型(集合)

if any("tk_dd_id" in s for s in collection):
        print("Found!")

'Collection' 对象不可迭代

type(collection)
<class 'pymongo.collection.Collection'>

Find tuple of strings in string in python

【问题讨论】:

  • 关于如何检查 pymongo 集合中的字符串的任何想法或解决方案?

标签: python string pymongo


【解决方案1】:

这个问题似乎没有实际意义,因为您必须将集合名称作为参数传递给Collection 对象的初始值。但如果您必须查询名称,您可以使用name 属性:

from pymongo import MongoClient
from pymongo.collection import Collection
from pymongo.database import Database

collection_name = "tk_dd_id"
collection = Collection(Database(MongoClient(host=['123234'], document_class=dict, tz_aware=False, connect=True, ssl=True, replicaset='mongomongo'), 'health_cards'), collection_name)

if "tk_dd_id" in collection_name:
    print("Found!")

if "tk_dd_id" in collection.name:
    print("Found!")

【讨论】:

猜你喜欢
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多