Uploaded image for project: 'Motor'
  1. Motor
  2. MOTOR-627

How to make count() in motor, trying to replace pymongo with motor

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.3
    • Component/s: asyncio
    • None

      I have some trouble.

      When I use pymongo everything works all right:
       {{import pymongo from pymongo import TEXT class Search(): # search in mongodbdef _init_(self, search): # connect to mongoDB self.search = search
      self.myclient = pymongo.MongoClient("mongodb+srv://:@cluster0.ye4cx.mongodb.net/DATABASE?retryWrites=true&w=majority&ssl=true&ssl_cert_reqs=CERT_NONE")
      self.mydb = self.myclient["info"]
      self.mycol = self.mydb["comics"] def searchdb(self): # finds all comics by request self.mycol.create_index([('title', TEXT)], default_language='english')
      self.find = self.mycol.find({"$text": {"$search": self.search}})
      print(self.find) if self.find.count() == 0: return 0else: return (self.find)}}
      But in motor AttributeError: 'AsyncIOMotorCursor' object has no attribute 'document_count'
       {{import motor.motor_asyncio class Search(): # search in mongodbdef _init_(self, search): # connect to mongoDB self.search = search
      self.myclient = motor.motor_asyncio.AsyncIOMotorClient("mongodb+srv://:@cluster0.ye4cx.mongodb.net/DATABASE?retryWrites=true&w=majority&ssl=true&ssl_cert_reqs=CERT_NONE")
      self.mydb = self.myclient["info"]
      self.mycol = self.mydb["comics"] async def searchdb(self): # finds all comics by request self.mycol.create_index([('title', 'text')], default_language='english')
      self.find = self.mycol.find({"$text": {"$search": self.search}}) if self.find.document_count() == 0: return 0else: return self.find}}
       

            Assignee:
            shane.harvey@mongodb.com Shane Harvey
            Reporter:
            fungalspores@protonmail.com Fungal Spores
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: