Home > Software engineering >  How to check if string is in pymongo.collection.collection object
How to check if string is in pymongo.collection.collection object

Time:09-30

I'm searching for a solution to this but could not find any practical one

lets say we have a pymongo collection string and we are searching for string named "tk_dd_id" in it.

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

I tried

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

'Collection' object is not iterable

type(collection)

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

'Collection' object is not iterable

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

Find tuple of strings in string in python

CodePudding user response:

The question seems moot because you have to pass the collection name as a parameter to initial the Collection object. But if you must query the name you can do it with the name attribute:

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!")
  • Related