Mongodb as event-stream database (Change Stream)

hipster' Santos
3 min readDec 18, 2021

--

Did you know that you can use mongodb acting like event-driven system?
yes you can , in this article I'll give you a hands-on guid how set up it , and explain how this mongodb Change stream works .

First of all what is change stream : so the idea behind change stream is like listen to every change that is made in your collection . By changes I mean some operation that occur on your database collection like "insert","delete", "update".

with change stream is easier to cath'em 'cause mongodb use what is called as watcher which is base-on event-emitter.

If you know how event-emitter works you can understand quickly the idea behind watch() method of collections.

Implementation :

the following implementation will be applied to production database , 'cause mongodb change stream uses mongos instead mongod in order to work also , mongod only uses one instance of database by use change stream it'll use cluster of database , that means in order to make this work in production you need to set up replica-set .

In this implementaion I'll be using mongoose and nodemailer feel free to use mongodb drive .

for this example I'll emit an event whenever a user is added to Users' collection , this event will tell me to send anemail to that user that was already added to the Users'collection .

first we're gonna set up our schema , and then later we're gonna an email library to send email , heads-up "you can use any library you really wanna go with ".

Nodemailer config will as least as possible,without using smtp ,I'll just use a fake email just for example’s sake

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({

name: String ,

email: String,

password:String

});

const User = mongoose.model('user', userSchema);

the above code is just a user collection configuration

folder structure :
models/user.js ->contains the above code

const user = require('../models/user.js');

user.watch().on('change',async function (stream){

//the watch method accepd pipeline , and the event here is change is I said //change stream is base-on nodejs event-emitter

})

the stream output comes with this :

_id: {
_data: ‘8261BCA619000000032B022C0100296E5A10044FF86B4DF6D1443DAB6BCB4588A7D8F246645F6964006461BCA6199EA84E920B9498D80004’
},
operationType: ‘insert’,
clusterTime: Timestamp { _bsontype: ‘Timestamp’, low_: 3, high_: 1639753241 },
fullDocument: {
_id: 61bca6199ea84e920b9498d8,
level: ‘Básico’,
targetAudience: [ ‘summer’ ],
requirements: [ ‘nothing’ ],
isBuka: true,
approved: false,
softDelete: false,
classe: [],
name: ‘summer’,
imageURL: ‘summer’,
miniDescription: ‘summer’,
description: ‘summer’,
owner: 600d9b917e535199a598c91e,
createdAt: 2021–12–17T15:00:41.430Z,
updatedAt: 2021–12–17T15:00:41.430Z,
slug: ‘summer’,
__v: 0
},
ns: { db: ‘production’, coll: ‘courses’ },
documentKey: { _id: 61bca6199ea84e920b9498d8 }
}

Important to take in this object

documentKey- the key of current document where the event is being emitted

operationType- type of operation being performed other operations are ("inser","update", "delete")

fullDocument: if the operationType is 'insert ' this will present the current data that was added to the collection

updateDescription{updateFields}: if the operationType is "update" this will tell you which fields was updated .

alright having this in mind you can use the watch method to listen to any change related to insert or update and then sending a message to this user through email. can you do this as an exercise?

let's implement this:

--

--

hipster' Santos
hipster' Santos

Written by hipster' Santos

Fullstack developer , Distributed system engineer,Competitive programmer find at https://github.com/HipsterSantos

No responses yet