Mongoose pre findOneAndUpdate hook issues

I am trying to update pre hook counts. The problem is that for some unknown reason, the findOneAndUpdate quest findOneAndUpdate not have access to the document, as far as I can tell.

I would like to do this:

 source.pre('findOneAndUpdate', function (next) { console.log('------------->>>>>> findOneAndUpdate: '); this.objects = this.objects || []; this.people = this.people || []; this.events = this.events || []; this.objectCount = this.objects.length; this.peopleCount = this.people.length; this.eventCount = this.events.length; next(); }); 

But for some reason, this in hook is not a document, its Query object, which seems useless.

What am I missing? How to use a preliminary hook to update counters on findOneAndUpdate?

+7
mongoose
source share
2 answers

The documentation states:

The second query software differs from the document middleware in a subtle but important way: in the middle program document, this refers to the document being updated. In request middleware, mongoose does not necessarily refer to an updated document, so this refers to a request object, not an updated document.

The update action usually updates a document that exists only in the database (it tells the MongoDB server: β€œfind document X and set property X for value Z”), therefore the full document is not available for Mongoose and therefore you cannot update the counters (for which at least access to arrays whose length you want to determine is required).

Aside: why would you use the individual *Count properties in your schema anyway? If you want to request arrays matching a specific size, you can directly use the $size operator on arrays.

If you really need the count properties, then for each update you need to track the number of changes you have made to each of the arrays (in terms of the number of added / deleted items) and use $inc to configure the counters.

+5
source share

You can do smthng like this β†’

 source.pre('findOneAndUpdate', function (next) { console.log('------------->>>>>> findOneAndUpdate: '); this._update.$set.objects = []; this._update.$set.people = []; this._update.$set.events = []; next(); }); 

pay attention to _update. $ set, because in the context of "this" there will be a request. Therefore, you can easily add whatever you want!

+5
source share

All Articles