0

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.

1 Answer 1

2

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)

Sign up to request clarification or add additional context in comments.

1 Comment

I hadn't realized it was inside $set. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.