These are chat archives for Automattic/mongoose

21st
Dec 2017
Paul "Joey" Clark
@joeytwiddle
Dec 21 2017 08:44

@alavers Yes, mongoose is very handy for 1, but does not help with 2, as far as I know.

We do have a library function here. We pass it an update function (which performs the update). If the update function fails with a “mongodb died” message, then the library function will wait and then try to call the update function again.

One problem when a DB or network dies is that you don’t know for sure whether the update failed, or if the update succeeded, but the confirmation didn’t make it back. That is more problematic for some queries than for others.

Mohamed Dhaker Abdeljawed
@Mdhaker
Dec 21 2017 14:30

Hello, I'm using mongoose and trying to execute this

  User.findByIdAndUpdate(user._id, { $set: { trainings: newTrainingData }}, function (err, updatedTrainings)
            {
              if (err)
              {
                console.log(err);
              }
              res.send("Works fine !");
            });

I don't know why the update middle is not triggered

schema.pre("update", function() {
    console.log("update: ");
    this.update({}, {
        $set: {
            updatedAt: new Date()
        }
    });
});
Jenky W. Nolasco
@jenkynolasco11
Dec 21 2017 14:56
Maybe try schema.pre('findOneAndUpdate', function(next) { ... }) @Mdhaker
Andrew Lavers
@alavers
Dec 21 2017 15:23
@joeytwiddle This library function you mentioned: Is it a mongoose specific thing or is it a generic "make this function retry errors" wrapper? Does it wait until the db connection is reestablished before reattempting?
Mohamed Dhaker Abdeljawed
@Mdhaker
Dec 21 2017 17:45
is there a way to get the index of an embdedd doc in an array ?