Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-2939

Cursor Id error when using stream to watch for DB changes

    XMLWordPrintableJSON

Details

    • Icon: Task Task
    • Resolution: Works as Designed
    • Icon: Major - P3 Major - P3
    • None
    • None
    • None

    Description

      pymongo version 3.12.0
      I'm using watch to monitor db changes, but when a db change occur I get the error:

      cursor id CURSOR_ID_NUMBER is already in use
      

      I'm using it in Flask to stream db changes to the client side

      client = MongoClient(mongo_url)
      db_stream = client[DB_NAME][COLLECTION_NAME].watch([{ "$match": {'fullDocument.user_id':'{}'.format(CLIENT_ID)}}])
      

      And in Flask:

      def event_stream():
          change = False 
          try:
              change = next(db_stream)
          except Exception as e:
              print("change err: {}".format(str(e)))
          print("db change: {}".format(change))
          yield dumps(change)
       
       
      @app.route('/stream')
      def stream():
          db_change = event_stream()
          return Response('data: {}\n\n'.format(dumps(db_change)),
                                mimetype="text/event-stream")
      

       

       

       

      Attachments

        Activity

          People

            shane.harvey@mongodb.com Shane Harvey
            urieah.avni@gmail.com urieah avni
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: