Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-12248

Changestream documents for Node ignores event-based API

    XMLWordPrintableJSON

Details

    Description

      Description

      When we generated change streams examples for the Node.js driver, we provided at least two different approaches: using cursors as an iterator, and using the resulting cursor as an event emitter. The Event Emitter approach is far more idiomatic in node, and I think should be represented in our documentation.

      For reference, I'm looking here: https://docs.mongodb.com/manual/changeStreams/

      We recommend this as the basic example:

      const collection = db.collection('inventory');
      const changeStream = collection.watch();
      const next = await changeStream.next(); 

       

      But the preferred form is:

      const collection = db.collection('inventory');
      const changeStream = collection.watch();
      changeStream.on('change', doc => {
        // process next change document
      });

      Scope of changes

      Impact to Other Docs

      MVP (Work and Date)

      Resources (Scope or Design Docs, Invision, etc.)

      Attachments

        Activity

          People

            kay.kim@mongodb.com Kay Kim (Inactive)
            matt.broadstone@mongodb.com Matt Broadstone
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              5 years, 4 weeks, 1 day ago