title: Mongodb change stream with pipeline using mongoose
body: <p><strong>I'm attempting to listen for changes in my mongodb cluster, using change streams; however after following several tutorials, my final implementation using mongoose doesn't work. How can I use the current mongoose connection to listen for changes in the database</strong></p>
  
  <p><strong>mongoose connection:</strong></p>
  
  <pre><code>mongoose
  .connect(db, {
      useNewUrlParser: true,
      useFindAndModify: false,
      useUnifiedTopology: true
      // useCreateIndex: true
  })
  .then(() =&gt; {
      console.log("Connected to MongoDB...");
  })
  .catch(err =&gt; {
      console.log(err);
  });
  </code></pre>
  
  <p><strong>Change stream:</strong></p>
  
  <pre><code>const pipeline = { 
    $match: {
      $or: [{ operationType: 'insert' },{ operationType: 'update' }], 
      'fullDocument.institution': uniId 
    } 
  };
  
  const changeStream = Post.watch([pipeline], {fullDocument: 'updateLookup'});
  
  changeStream.on("change", next =&gt; {
          switch(next.operationType) {
            case 'insert':
              console.log('an insert happened...', "uni_ID: ", next.fullDocument.institution);
              let rooms = Object.keys(socket.rooms);
              console.log("rooms: ", rooms);
  
              nmsps.emit('insert', {
                type: 'insert',
                msg: 'New question available',
                newPost: next.fullDocument
              });
              break;
  
            case 'update':
              console.log('an update happened...');
  
              nmsps.emit('update', {
                type: 'update',
                postId: next.documentKey._id,
                updateInfo: next.updateDescription.updatedFields,
                msg: "Question has been updated."
              });
              break;
  
            case 'delete':
              console.log('a delete happened...');
  
              nmsps.emit('delete', {
                type: 'delete',
                deletedId: next.documentKey._id,
                msg: 'Question has been deleted.'
              });
              break;
  
            default:
              break;
         }
   })
  </code></pre>