Rahul
Rahul

Reputation: 2617

db.collection.findOneAndUpdate() is not working from terminal

My Data

{ "_id" : ObjectId("60c012cc35fd3c596d61e72d"), "tags" : [ "react", "node", "js" ], "title" : "This is the new edited title", "description" : "I am the best JS developer!,Description must contain atleast 200 words!,Description must contain atleast 200 words!,", "md" : "# Markdown", "type" : "sr", "createdAt" : 1623200460769, "authorId" : ObjectId("5f9a2c91f4c1090008e5237f"), "grantCreate" : [ ], "revokeCreate" : [ ], "edits" : [ ], "posts" : [ ], "__v" : 0 }

Query

db.blogs.findOneAndUpdate({_id:"60c012cc35fd3c596d61e72d"},{$set:{title:"This is 788"}})

Result

null

I am executing this from the terminal, where it is not working. But, it is working from the nodejs and mongoose library.

The entire process

 db.blogs.find()
{ "_id" : ObjectId("60c012cc35fd3c596d61e72d"), "tags" : [ "react", "node", "js" ], "title" : "This is the new edited title", "description" : "I am the best JS developer!,Description must contain atleast 200 words!,Description must contain atleast 200 words!,", "md" : "# Markdown", "type" : "sr", "createdAt" : 1623200460769, "authorId" : ObjectId("5f9a2c91f4c1090008e5237f"), "grantCreate" : [ ], "revokeCreate" : [ ], "edits" : [ ], "posts" : [ ], "__v" : 0 }
> db.blogs.findOneAndUpdate({_id:"60c012cc35fd3c596d61e72d"},{$set:{title:"This is 788"}})
null
> db.blogs.find()
{ "_id" : ObjectId("60c012cc35fd3c596d61e72d"), "tags" : [ "react", "node", "js" ], "title" : "This is the new edited title", "description" : "I am the best JS developer!,Description must contain atleast 200 words!,Description must contain atleast 200 words!,", "md" : "# Markdown", "type" : "sr", "createdAt" : 1623200460769, "authorId" : ObjectId("5f9a2c91f4c1090008e5237f"), "grantCreate" : [ ], "revokeCreate" : [ ], "edits" : [ ], "posts" : [ ], "__v" : 0 }

Upvotes: 0

Views: 32

Answers (1)

vikasThakur.com
vikasThakur.com

Reputation: 578

It is apparently not able to find the document because of _id value being of ObjectId type not string type.

try this:
db.blogs.findOneAndUpdate({_id:ObjectId("60c012cc35fd3c596d61e72d")},{$set:{title:"This is 788"}})

Upvotes: 2

Related Questions