Automatic self-populating field

Hi all,

Quick question. I’m running the standalone (5.0.7) version of MongoDB.
Is there a way of creating an automatic self-populating field within a collection. IE. Similar to the _id field, but user definable.

Thanks.

Hi @Andy_Bryan,

In this blog post, I’m using MongoDB Realm Triggers to populate an entire object automatically from an external API. You could use change streams to do the same thing and populate your custom field(s) based on an event (insert, update, replace, delete).

https://www.mongodb.com/developer/how-to/data-enrichment-stitch/

Cheers,
Maxime.

1 Like

Hi there @MaBeuLux88 ,

Thanks for the response. Is this only possible on MongoDB Atlas, or is it also possible on the stand alone version?

Thanks.

Andy.

A stand-alone node would have to be transformed into a single node Replica Set (RS) because Change Streams (like a few other MongoDB features) rely on the oplog that is only available if you are running a Replica Set.

Atlas Triggers are “just” a way to run serverless functions each time an event happens. These events are generated from Change Streams that rely on the underlying oplog collection.

So you can totally work with Change Stream in a Community single node RS, but you’ll have to write your own program to listen to the change stream and start some code each time an event happens. Eventually multi-thread that program if you don’t want to queue the events. Also you will probably need a restart mechanism (change stream have that built-in) so you don’t restart from scratch if the program stops.

Cheers,
Maxime.

2 Likes

Brilliant. Thanks for your help.

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.