Mongoose - findOneAndUpdate not updating document in MongoDB
13:56 13 Dec 2025

Summary

I am trying to update an array field (images) in a MongoDB document using Mongoose findOneAndUpdate.

The update appears in the returned document logged in my Node.js app, but the change is not persisted in the database.

However, running the same update query directly in the MongoDB shell works correctly.

Schema

{
    ...
    slug: { type: String },
    images: [{ type: String }] 
}

Expected Results

Before running the code:

{
    slug: 'Example',     
    images: ['ImgExample1'] 
} 

After Running Code:

{
    slug: 'Example',     
    images: ['ImgExample1', 'ImgExample2'] 
}

Actual Results

Before and After running the code:

{
    slug: 'Example',     
    images: ['ImgExample1'] 
}

But inside the Node.js app, the returned document from findOneAndUpdate logs as:

{
    slug: 'Example',
    images: ['ImgExample1', 'ImgExample2']
}

Repository Code

public async updateServiceImages(slug: string, imageUrls: string[]): Promise> {
        try {
            const service = await this._service.findOneAndUpdate(
                { slug },
                {
                    $addToSet: {
                        images: { $each: imagesUrls }
                    }
                },
                {
                    new: true,
                    upsert: true
                }
            );

            if (!service) throw new NotFoundError(`Service with slug '${slug}' not found`);

            console.log(service)

            return {
                data: null,
                message: 'Service images updated successfully',
                error: false,
            }
        } catch (err: unknown) {
            logger.error(`UPDATE SERVICE IMAGES: ${getErrorMessage(err)}`);
            throw err;
        }
    }

Why does findOneAndUpdate return a document with the updated array, but the change is not persisted in MongoDB?

Is this a known Mongoose behavior or configuration issue?

node.js mongodb express mongoose findoneandupdate