Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
findOneAndUpdate is not working in mongodb

Here is code to update db.

let profileUrl = 'example'
UserSchemaModel.findOneAndUpdate({_id:userId}, {$set: {profileUrl:profileUrl} }, {new:true})
.then((updatedUser:UserModel) => {
     console.log(updatedUser.profileUrl)
})

result:undefined

As you can see in the result, profileUrl field is not updated. so I have inserted overwrite option.
But it didn't work. Following error is appeared.

src/controllers/user.ts(267,106): error TS2345: Argument of type '{ new: true; overwrite: boolean; }' is not assignable to parameter of type 'ModelFindOneAndUpdateOptions'.
Object literal may only specify known properties, and 'overwrite' does not exist in type 'ModelFindOneAndUpdateOptions'.

UserSchema is same to following.

{
    name:String,
    thumbnailImg:String,
    email:String,
    phone:Number,
    profileUrl:String
}

now saved data is same following.

[{
    _id:ObjectId("5ffe07805cbab00019cdbbcd"),
    name:"Jhon",
    thumbnailImg:"thumbnail_image_url",
    email:"jhon@gmail.com",
    phone:"1212121212",
}]

I have tried several times, but the result is same.
How can I fix this issue? I think there are methods to fix this issue.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You are trying to match using the _id field which is defined as ObjectId type in your schema. So in your query, try searching using the ObjectId type.

let ObjectId = require('mongoose').Types.ObjectId; 
let profileUrl = 'example'
UserSchemaModel.findOneAndUpdate({_id: new ObjectId(userId)}, {
     $set: {
          profileUrl: profileUrl
     } 
}, {new:true}).then((updatedUser:UserModel) => {
     console.log(updatedUser.profileUrl)
})
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda