Send notification to user when document is inserted

Hello everyone. How I can notify some specific users that a new Document is inserted or a field inside the document is changed. For example: when a user adds a new Order, the respective Restaurant will be notified on their phone.

Hey, I would highly recommend looking into Database Triggers. Attached are some helpful links:

  1. https://www.mongodb.com/developer/products/atlas/triggers-tricks-data-driven-schedule/
  2. https://www.mongodb.com/docs/atlas/app-services/triggers/database-triggers/

You could define a trigger on the collection for “insert” events or “update/replace” events and execute a function that either (a) calls out to a push notification service or (b) adds data to a secondary collection that is being listened to on the device

what are the services that I can use for Mongodb and Realm?

Also, do you have any example on how I can send push notification to specifics users?

You can write a function that will be run within the “Trigger” such that it runs on every insert to the collection and performs some action (hit an endpoint, update a document in mongodb, etc). Here are some examples:

  1. https://www.mongodb.com/docs/atlas/app-services/functions/mongodb/write/

Here is a nice article detailing how to use push notifications:

Thanks for the info. In the article Firebase is being used. Does MongoDB has something similar to what Firebase do? I know that change streams exists, but they consume some memory and they have to be connected every time. Does MongoDB have anything else implemented? Can I send push notification to a specific user directly in a trigger function?

I would like to take a very different approach to this problem. If you are using Kotlin SDK, you can setup stream of information flowing into the app whenever a change occur something like [this].(Job-Tracker/RealmRepo.kt at 6fde3d672b20991c417057a7c8a775f717e1cf88 · mongodb-developer/Job-Tracker · GitHub)

And once you have the update, trigger a local push notification or toast to inform user about the change. In the code sample shared above I am adding a toast whenever new Job is interested into the document.

Do let me know if this helps.

1 Like

Thanks for the response. Its a great idea! I have some questions:
I do not need any trigger for this?
It works for adding a new document or updating a field?

No triggers are required for this. Do check this out.

1 Like

Does someone know if this would work if my app is not open or in standby mode?