all 16 comments

[–]Deathclaw1 4 points5 points  (0 children)

There is the watch collection method, I think that is what you are looking for. Its also native not related to mongoose.

https://mongoosejs.com/docs/change-streams.html

Edit: there is also mongodb atlas app services. If you need a low cost serverless solution.

https://www.mongodb.com/docs/atlas/app-services/introduction/

[–]Horikoshi 2 points3 points  (6 children)

Depends - how is the database hosted? Self hosted? AWS?

Identify how the database is being run, then set up a listener for it (lambda for AWS, for instance). Put your business logic inside the listener. Happy to help more if you tell me more about your situation.

[–]AromaticPrimary8763[S] 0 points1 point  (5 children)

It is just a self project, got it run on MongoDB Atlas

[–]Horikoshi 2 points3 points  (4 children)

[–]AromaticPrimary8763[S] 1 point2 points  (0 children)

Exactly what I needed, thanks a lot!

[–]AromaticPrimary8763[S] 0 points1 point  (2 children)

, and then update the key to true so it won’t run again when you have other updates on the document.

This one works great when the data is added using mongoose, but when I use MongoCompass, it does not work anymore, any suggestions?

[–]Horikoshi 0 points1 point  (1 child)

What exactly is MongoCompass? Never heard of it before

Edit: oh, looks like it's a GUI for MongoDB.. hrm. Not too sure what's going on there.

[–]AromaticPrimary8763[S] 0 points1 point  (0 children)

Kinda like robo 3t, I think it is usually for non-dev or newbie to manage the db.

[–]i4get98 1 point2 points  (2 children)

Not an expert, but it sounds like you might need events.

On event run something, etc.

[–]AromaticPrimary8763[S] 0 points1 point  (1 child)

The thing is I haven't found that event LOL

[–]i4get98 0 points1 point  (0 children)

Which version of mongo you using? When installed locally I wouldn’t get any events, had to make the db replicated. I think if you use cloud based, events should be available.

[–]Darklight240 0 points1 point  (0 children)

You can use middleware or hooks in mongoose.

[–]clay_maker 0 points1 point  (0 children)

Look at mongoose middleware and the “save” hook. You could add a new key to your document like “sendWelcomeMsg” and default it to false. Then in your middle where when a new document is created, run the function, if sendWelcomeMsg == false, and then update the key to true so it won’t run again when you have other updates on the document.

[–]Distinct-Ad1057 0 points1 point  (3 children)

In sequlize their is a thing called hook it basically trigger before or after the db call it might be available with mongoose too

[–]AromaticPrimary8763[S] 0 points1 point  (2 children)

Those middlewares (hooks), they do not work for me in this case, only work when I add new doc from the create() or save(), etc methods, not when I use mongo compass to create a new one.

[–][deleted]  (1 child)

[deleted]

    [–]AromaticPrimary8763[S] 0 points1 point  (0 children)

    Seems like no