0

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

Alexander
  • 4,527
  • 5
  • 51
  • 98

1 Answers1

1

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!")
Belly Buster
  • 8,224
  • 2
  • 7
  • 20
  • Thanks! I did not know we can access the `collection` name with `collection.name`. Do you know any documention about this because I could not find it :_) – Alexander Sep 29 '21 at 17:47
  • https://pymongo.readthedocs.io/en/stable/api/pymongo/collection.html#pymongo.collection.Collection – Belly Buster Sep 29 '21 at 20:48