Mongoose instance .save() not working when embedded array object changed

前端 未结 3 1368
天命终不由人
天命终不由人 2021-01-17 23:47

I am using Mongoose npm module to manage mongodb. This is schema of mongodb collection what I am going to update.

var UserSchema = new Schema({
    username:         


        
相关标签:
3条回答
  • 2021-01-18 00:30

    If you just want to update cards based on cardIndex:

    User.update({_id: userID}, {'$set': {
        'cards.cardIndex': cardInfo
    }}, function(err) {
       //code
    }
    
    0 讨论(0)
  • 2021-01-18 00:33

    Thanks for all answers. I find this solution in addition.

    doc["cards"].set(cardIndex, cardInfo)
    

    Cheers!

    0 讨论(0)
  • 2021-01-18 00:43

    The problem is that mongoose don't knwo your array is modified.

    You can use 2 solutions :

    markModified

    This function will mark the embedded element as modified and force a resave of it. It will tell mongoose to resave this element.

    User.findById(userID).exec(function (err, doc) {
            let cardInfo = req.cardInfo
            let cardIndex = req.cardIndex
            doc["cards"][0] = cardInfo;
            console.log(doc)
    /*  here I got right doc object as I requested
    {
            "_id": "59f3bdd488f912234fcf06ab",
            "email": "test@gmail.com",
            "username": "test",
            "__v": 2,
            "cards": [
                {
                    "testNo": "42424242424242"
                }
            ]
        }
    */
            doc.markModified('cards');
            doc.save(function (err) {
              if (err) {
                return res.json({
                  success: false,
                  msg: 'Card add error'
                });
              }
              res.json({
                success: true,
                msg: 'Successful updated card.'
              });
            });
    })
    

    Use a full schema.

    To avoid the markModified trick, you should describe the content of cards in your schema. This way mongoose will be able to determine if it needs to save the field or not.

    Here is the way to declare your schema properly :

    const CardSchema = new Schema({
      testNo: String,
    });
    
    var UserSchema = new Schema({
        username: {
            type: String,
            unique: true,
            required: true
        },
        email: {
            type: String,
            unique: true,
            required: true
        },
        cards: [CardSchema]
    });
    module.exports = mongoose.model('User', UserSchema);
    

    This way, mongoose will be able to detect if a value inside cards changed and save only the modified item.

    If you can do it (static schema), this is clearly the good way to do it.

    0 讨论(0)
提交回复
热议问题