Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

320
Views
Mongo DB $push object wraps it in an array first

When I use $push in mongodb, the expected outcome turns out differently. It actually wraps the object I want to push in an array. This is problematic because I'd have to map over the result just to extract it. Any help would be greatly appreciated.

My Mongo Query

const pushAction = {
    $push: {
      cart: {
        id: itemId,
        quantity: quantity
      }
    }
  }

  // Add item to user's cart
  User.update({_id: userId}, pushAction, (err, success) => {
    if (err) {
      res.status(422).json({'error': 'There was a problem adding the item to your cart.'});
    }
    if (success) {
      // Find user and return the cart
      User.findOne({_id: userId}, {cart: 1}, (err, user) => {
        res.status(200).json({'message': 'The item was successfully added to your cart.', cart: user.cart});
      })
    }
  });

User Schema

// Define User Model
const userSchema = new Schema({
  firstName: {
    type: Schema.Types.String,
    required: true
  },
  lastName: {
    type: Schema.Types.String,
    required: true
  },
  password: {
    type: Schema.Types.String,
    required: true
  },
  email: {
    type: Schema.Types.String,
    required: true
  },
  cart: {
    type: Schema.Types.Array
  },
  dateCreated: {
    type: Schema.Types.Date,
    default: Date.now,
    required: true
  },
  dateUpdated: [
    {
      date: {
        type: Schema.Types.Date
      },
      details: {
        type: Schema.Types.ObjectId
      }
    }
  ],
  verified: {
    type: Schema.Types.Boolean,
    required: true
  },
  role: {
    type: Schema.Types.String,
    default: ROLES_BASIC_USER
  }
});

Expected Outcome

"cart" : [ 
    {
        "id" : "587b6b69799ad7ff650edbb5",
        "quantity" : 1
    }, 
    {
        "id" : "587b6b69799ad7ff650edbb5",
        "quantity" : 1
    }, 
    {
        "id" : "587b6b69799ad7ff650edbb5",
        "quantity" : 1
    }
],

Actual Result

"cart" : [ 
        [ 
            {
                "id" : "587b6b69799ad7ff650edbb5",
                "quantity" : 1
            }
        ], 
        [ 
            {
                "id" : "587b6b69799ad7ff650edbb5",
                "quantity" : 1
            }
        ], 
        [ 
            {
                "id" : "587b6b69799ad7ff650edbb5",
                "quantity" : 1
            }
        ]
    ]
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

// Define User Model
const userSchema = new Schema({
  firstName: {
    type: Schema.Types.String,
    required: true
  },
  lastName: {
    type: Schema.Types.String,
    required: true
  },
  password: {
    type: Schema.Types.String,
    required: true
  },
  email: {
    type: Schema.Types.String,
    required: true
  },
  cart:[ {
    id: Schema.Types.ObjectId,
    quantity: Number 
  }],
  dateCreated: {
    type: Schema.Types.Date,
    default: Date.now,
    required: true
  },
  dateUpdated: [
    {
      date: {
        type: Schema.Types.Date
      },
      details: {
        type: Schema.Types.ObjectId
      }
    }
  ],
  verified: {
    type: Schema.Types.Boolean,
    required: true
  },
  role: {
    type: Schema.Types.String,
    default: ROLES_BASIC_USER
  }
});
over 4 years ago · Santiago Trujillo Report

0

Try changing pushAction as follows:

const pushAction = {
    $push: {
        cart: { $each: [ {id: itemId, quantity: quantity } ] }
    }
}

Clean existing items in cart field before trying this. If it still fails then the issue might be with the schema.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!