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

195
Views
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 answers
Answer question

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 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!