W
W
WAYNEDEV2020-04-14 19:59:39
Node.js
WAYNEDEV, 2020-04-14 19:59:39

How to make action on change in mongodb node.js?

Hello!
Is there any way to watch the collection change and call the callback? I googled for a long time and found nothing
. Thanks a lot in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-04-14
@WAYNEDEV

it seems not, BUT you can specify in the scheme that a function be executed before saving the document, it is called pre-save hooks hooks or something like that, the
documentation is rather muddy, but there is
https://mongoosejs.com/docs/middleware.html#pre

const schema = new mongoose.Schema({ name: String });

// Mongoose will call this middleware function, because this script adds
// the middleware to the schema before compiling the model.
schema.pre('save', () => console.log('Hello from pre save'));   // вот это

// Compile a model from the schema
const User = mongoose.model('User', schema);

new User({ name: 'test' }).save();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question