Struggling to update a document in my mongoose server using findOneAndUpdate
. I've set it up as a PUT request with the following definition:
public updateConfigWithType(req: Request, res: Response) {
configs.findOneAndUpdate({'companyInfo.uniquecompanyid': req.params.id}, { $set: { companyName: "ITSWORKING" }} ,(err,doc) => {
if (err) {
res.send(err);
}else {
res.json({
message: 'Succesfully updated this item'
});
}
})
}
The PUT request URL is formatted like this:
http://localhost:3000/parserConfig/123123wfdwedfwfwefwef
Here's a snippet of the JSON data from my mongoose documents:
{
"_id": {
"$oid": "5bbf27ad4cf8a6be65ea6257"
},
"companyInfo": {
"companyName": "example",
"hashKey": "sdfsdf",
"hashFunction": "sdfsdf",
"uniquecompanyid": "123123wfdwedfwfwefwef"
},
"version": "1",
"__v": 0,
"parserConfig": {
"id": {
"fieldName": "key",
"path": "key"
},
The goal is to retrieve the correct document by its uniquecompnayid in the URL and specifically update the parserConfig
field with the content of req.body
. At the moment, sending a PUT request returns:
{"message":"Succesfully updated this item"}
However, no actual changes are made. What could be causing this issue?
Update
In an attempt to troubleshoot, I modified the code like so:
public updateConfigWithType(req: Request, res: Response) {
var query = {'companyInfo.uniquecompanyid':req.params.id};
configs.findOneAndUpdate(query, {$set:{'companyInfo.uniquecompanyid':"heheheheh"}}, {new:true}, function(err, doc){
if (err) {
return res.send(err);
}
return res.send(doc);
});
}
Despite these adjustments, Postman still displays the old document without any updates.
UPDATE after spyros request
10:26:34 PM web.1 | Mongoose: configs.findOne({ 'companyInfo.uniquecompanyid': '123123wfdwedfwfwefwef' }, { new: true, projection: {} })
10:26:34 PM web.1 | PUT /parserConfig/123123wfdwedfwfwefwef 200 158.118 ms - 2089
UPDATE new information notice
Recently came across this reference: https://github.com/Automattic/mongoose/issues/7011
It mentions:
When calling findOneAndUpdate with an update doc property not in the schema, the debug output shows potential issues. Why does updating work through Studio 3t but not via custom code?