Reputation: 3417
I am trying to update one of my documents stored in mongoose server by using findOneAndUpdate
. I set it as a PUT request and this is how I defined it.
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'
});
}
})
}
My PUT request URL is setup like this: http://localhost:3000/parserConfig/123123wfdwedfwfwefwef
This is a little piece of the the JSON data that exits in my documents in my mongoose server:
{
"_id": {
"$oid": "5bbf27ad4cf8a6be65ea6257"
},
"companyInfo": {
"companyName": "example",
"hashKey": "sdfsdf",
"hashFunction": "sdfsdf",
"uniquecompanyid": "123123wfdwedfwfwefwef"
},
"version": "1",
"__v": 0,
"parserConfig": {
"id": {
"fieldName": "key",
"path": "key"
},
So, I want to be able to find the right document by the uniquecompnayid that is in the URL and I specifically want to UPDATE the parserConfig
field with what exists in req.body
. I hardcoded everything for now for testing purposes.. Sending a PUT request currently gives me this answer in Postman:
{"message":"Succesfully updated this item"}
But nothing has been updated. What am I missing?
Update
I changed the code for testing purposes... This is what i tried:
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);
});
}
The response in postman is the OLD document. Nothing has been changed
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
I came across this: https://github.com/Automattic/mongoose/issues/7011
where it says:
I noticed that when calling findOneAndUpdate with an update doc property that isn't in the schema, the debug output shows the (wrong)? collection method.
Why would it work to update the same property through Studio 3t but not through my code?
Upvotes: 2
Views: 6749
Reputation: 11
I had this issue recently, the answer for me was that findOneAndUpdate(query, rule)
is asynchronous, so you have to await
it or call .then()
Eg.
var savedObj = await MyObj.findOneAndUpdate(query, newobj)
Upvotes: 0
Reputation: 13
I also ran into similar problem with findOneAndUpdate. After going through a truckload of write-ups and videos, what I found out was that Mongoose's schema constructor supports a second options argument.
One of the these options is "strict", which is meant enforced values passed to mongodb through the model instance is the same as what is specified in the schema.
If the option is set with the value pair { strict: true }, the update will fail if there is any difference between the schema and the values passed in. To override this behavior, strict should be set to false.
As an aside, none of the stuff I read made mention of this options. And I couldn't figure out what was going on until I read the documentation here: https://mongoosejs.com/docs/2.7.x/docs/schema-options.html
Upvotes: 1
Reputation: 45
As of Mongoose v7.6.2, the documentation states that:
The strict option, (enabled by default), ensures that values passed to our model constructor that were not specified in our schema do not get saved to the db.
Same as you, I am unable to define a schema for the collection since my documents vary a lot. What did the trick for me is adding strict: false
to the schema of the collection.
import { Schema } from 'mongoose';
const DynamicDocumentSchema = new Schema(
{ bar: String },
{ strict: false }
);
In case anybody's wondering, you can also add strict: false
to the update query just like below. This overrides whatever the value of strict
is in the schema.
await DynamicDocumentModel.findByIdAndUpdate(
docId,
{
$set: <whatever_update>
},
{ strict: false }
);
Upvotes: 2
Reputation: 196
Enable debug-logging:
mongoose.set('debug', true);
. This will show you the queries that are executed. I think the error may be related in some misspelling of some attribute or different object-id.
If that doesn't help, update your question with information of the debug-result so i can update my answer.
Upvotes: 2