Search code examples
javascriptmongodbexpressmongoosemongodb-update

Mongoose - Can't push array in subdocument


I'm trying to update an array by pushing it through findOneAndUpdate but I can't get it to update:

This is the code with which I try to push but does not make any movement:

let pettyCashItem = await PettyCashItems.findOneAndUpdate({
    _id: id, 
    "items._id": idItem },
{
    $set: { 
        "items.$.concept": req.body.concept,
        "items.$.incomeAmount": req.body.incomeAmount,
        "items.$.description": req.body.description,
        "items.$.expenseAmount": req.body.expenseAmount
        $push: {
            'items.$.lastModificationBy': {
                "uid": req.uid,
                "username": req.user.username,
                "comments": req.body.comments
            }
        }
    }
}, { new: 'true'});

This is my model:

const PettyCashItemsSchema = Schema (
  {
    items:[{
        concept: {
            type: String,
            maxlength:50,
            required: [true, 'El Concepto es obligatorio']
        },
        incomeAmount:{
            type: Number,
            maxlength:50,
            default:0,
            required: [true, 'El Ingreso es obligatorio']
        },
        expenseAmount:{
            type: Number,
            maxlength:50,
            default:0,
            required: [true, 'El Egreso es obligatorio']
        },
        description: {
            type: String,
            maxlength:50,
            required: [true, 'La Observación es obligatoria']
        },
        status: {
            type: Boolean,
            default: true,
            required: [true, 'El Estatus es obligatorio']
        },
        createdAt: {
            type: Date,
            default: Date.now
        },
        lastModificationBy: [{
            uid:{
                type: String,
                required:true
            },
            username:{
                type: String,
                required:true
            },
            date:{
                type: Date,
                default: Date.now
            },
            comments: {
                type: String,
                maxlength:300,
                required: [true, 'El Comentario es obligatorio']
            }
        }]
    }]

The update using $set for the other objects is correct but when trying to do a $push in the array this never works.

Thanks.


Solution

  • Try placing $push at same level with $set instead of inner level of $set.

    let pettyCashItem = await PettyCashItems.findOneAndUpdate({
        _id: id, 
        "items._id": idItem },
    {
        $set: { 
            "items.$.concept": req.body.concept,
            "items.$.incomeAmount": req.body.incomeAmount,
            "items.$.description": req.body.description,
            "items.$.expenseAmount": req.body.expenseAmount
        },
        $push: {
            'items.$.lastModificationBy': {
                "uid": req.uid,
                "username": req.user.username,
                "comments": req.body.comments
            }
        }
    }, { new: 'true'});
    

    Sample Mongo Playground (Query)