How to create collections in MongoDB Compass with Pre-image support💤

How to create collections in MongoDB Compass with Pre-image support💤

If you need 'fullDocumentBeforeChange' support in your triggers in change Events then you need to enable this feature while creating the collection.

For Single Collection Creation with Pre-image support👩🏼‍🤝‍👨🏻

We'll write commands to this area. Just drag it up.

Step 1: Switch to the database where you want to create your collections 🔁

use <db-name>   // use to switch your database

Step 2: Execute the following command to create the collection with pre-image support 🫂

db.createCollection("<collection-name>", {changeStreamPreAndPostImages: {enabled: true}})

As you can see my new collection has been created with pre-image support


Alternatively, If you need to create multiple collections with pre-image support then follow the steps🥷:

Doing these steps again and again for multiple collections is not ideal. There's a better way to do this.

You can use JavaScript forEach method to create multiple collections in one go.

["coll1", "coll2", "coll3", "coll4", "coll5"].forEach(collection => db.createCollection(collection, {changeStreamPreAndPostImages: {enabled: true}}))

As you can see, multiple collections with pre-image support has been created successfully

Conclusion 🥱

It's very important to enable pre-image support sometimes while using triggers. If you know these methods then our life become a lot easier sometimes.

My Github🙃: AryanKuAg

My LinkedIn🙂: Aryan Agrawal

Thanks for Reading🙏.