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

135
Views
Mongoose FindOneAndUpdate not Updating when Pushing an Object to a Nested Array

When trying to push to a nested array it's not updating in my database with the following code:

const obj = {
      key: key,
      user: user,
      description: description,
      date: Date.now(),
      guildId: guildId,
    };

    const guild = await this.GuildModel.findOneAndUpdate(
      { guildId: guildId },
      { $push: { 'guildData.commandLogs': obj } },
    );

My schema:

const GuildSchema = {
  guildId: { type: String },
 
  guildData: {
    commandLogs: [CommandLogsSchema],
  },

Any idea why my database isn't updating?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think you might have some typos in your model:

 const mongoose = require('mongoose'); 
 const Schema = mongoose.Schema;
 
 const GuildSchema = new Schema({   guildId: { type: String },  
 guildData: {
    commandLogs: [CommandLogsSchema],   })

in your controller, the filter condition that you're passing is not clear, which guildId you are using? also when you call the guildData.commandLogs field to update it you are missing brackets

const filter = { guildId: heretheactualId };
const update = {  { $push: { commandLogs: obj} }};

  const guild = await this.GuildModel.findOneAndUpdate(
       filter,
       update,
    );

this is not going to fix your problem because I don't know what kind of errors you're having or your database model but these tips can already help.

about 4 years ago · Juan Pablo Isaza 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!