These are chat archives for Automattic/mongoose

19th
Oct 2018
Berian
@chaiwa-berian
Oct 19 2018 10:01
Hi everyone! Just a quick question: What happens when I use the Model.updateMany() method to update multiple documents based on a list of ids and one of them is not matched? Does the method return some status to indicate if all were updated or some failed or couldn't match? Thanks!
Kev
@lineus
Oct 19 2018 10:38
@chaiwa-berian in your update query condition, are you doing something like { _id: { $in: arrayOfIds } } ?
Kev
@lineus
Oct 19 2018 11:09
The default behavior of mongodb is just to update the ones that match $in elements and ignore the ones that don't.
Kev
@lineus
Oct 19 2018 12:11
You can use a post-updateMany hook to store the number of Ids in the query condition from the updateMany call on the commandResult, or even return an error if the number of attempted matches doesn't match the number modified. Here's an example gist of mutating the commandResult and here's an example of returning an error from the post hook
Berian
@chaiwa-berian
Oct 19 2018 13:21
@lineus Thank you for the resources and good explanation! Very helpful...
Berian
@chaiwa-berian
Oct 19 2018 14:42

You can use a post-updateMany hook to store the number of Ids in the query condition from the updateMany call on the commandResult, or even return an error if the number of attempted matches doesn't match the number modified. Here's an example gist of mutating the commandResult and here's an example of returning an error from the post hook

@lineus, I have tried implementing this but the schema.post('updateMany', callback) hook is not being fired at all. Here is my code :

SILCGroupSchema.post('updateMany', function(error, commandResult, next){

    if(this.getQuery()._id.$in.length !== commandResult.result.nModified) {
        return next( new Error('not all groups were matched and updated'))
    }
    if(error){
        return next(error);
    }
    return next();
});
Kev
@lineus
Oct 19 2018 14:45
@chaiwa-berian what version of mongoose are you using?
Berian
@chaiwa-berian
Oct 19 2018 14:46
@lineus mongoose@5.2.12
Kev
@lineus
Oct 19 2018 14:50
@chaiwa-berian The. updateMany hook is available on 5.2.12, are you calling updateMany on the model associated with that schema or perhaps on another model?
the other thing to look out for, is that you are declaring the hook before you compile the schema into a Model with mongoose.model()
Berian
@chaiwa-berian
Oct 19 2018 14:52
@lineus This is how I am calling the update method:
var test_array = ['5bc9a3b98b65c9fa80766019','5bc9a3b98b65c9fa80766013']
//1. Push the member to each of the groups listed in the array, synchronously
await SILCGroup.updateMany({_id: { $in: test_array}},{ $push: { members: silc_group_member._id}}).catch(error=>{
return next(error);
});
@lineus Should I declare after compiling?
Berian
@chaiwa-berian
Oct 19 2018 14:59
@lineus I am connecting to a replicaSet on localhost, could that contribute? :)
@lineus I changed the callback signature from function(error,commandResult, next) to function(commandResult, next) as exactly as it looks in your code and it works! Not sure why it doesn't work with the error argument since the docs provides for it?
Kev
@lineus
Oct 19 2018 15:26
the hook callbacks are a little different from the query callbacks. I'm so used to seeing error as the first parameter I didn't catch it in your example above :) I'm glad you figured it out.
all hooks, virtuals, etc should be added to the schema before compiling it to a model, apologies my earlier comment was vague :)
Berian
@chaiwa-berian
Oct 19 2018 15:28
@lineus Thank you, appreciated! Everything looks good, now!
Kev
@lineus
Oct 19 2018 15:28
anytime :smile:
Berian
@chaiwa-berian
Oct 19 2018 15:29
:smile:
Berian
@chaiwa-berian
Oct 19 2018 16:35
Hello. How do I end the request for a given endpoint if an error occurs? See my code below which is proceeding to the next line of code even after encountering the error and returning it to the error handler:
await silc_group_member.validate().catch(error => {


        res.status(422); 

         return next(error);  //return error and end request, but only returns error and goes ahead and executes the next line too!

    });


    await SILCGroup.updateMany({ _id: { $in: silc_group_member.silc_groups }},{ $push: { members: silc_group_member._id }}).catch(error => {

        res.status(422); 

        return   next(error); //Same thing happens here
    });
Berian
@chaiwa-berian
Oct 19 2018 19:45
Hello. How can I create a custom validator that checks that a given array field on the schema should not be empty?
I have tried the below, when silc_groups = [] nothing works and the validator is never fired:
silc_groups: [{ 
        type: Schema.Types.ObjectId, 
        ref: 'SILCGroup', 
        required: true,
        validate: {

            validator: function(v){
               if(v === undefined)
                    return false
                else
                    return true
            },
            message: "silc_groups field values must be valid existing group ids"
        }
    }]