Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

findByIdAndUpdate not updating #1060

Closed
hallas opened this issue Aug 18, 2012 · 6 comments
Closed

findByIdAndUpdate not updating #1060

hallas opened this issue Aug 18, 2012 · 6 comments

Comments

@hallas
Copy link

hallas commented Aug 18, 2012

User.findByIdAndUpdate(req.body._id, {
$set: {
name: req.body.name
}
}, { upsert: true },
function (err, user) {
return res.json(err == null ? user : err);
}
);

This code does not work. It simply returns the document without the updated name field. This means that the _id is correct, but what's not working? I tried alot of different combinations.

@aheckmann
Copy link
Collaborator

I cannot reproduce on v3.0.2. Which version of Mongoose/MongoDB/Node are you using? What are the before/after values you are updating?

@hallas
Copy link
Author

hallas commented Aug 19, 2012

I found out why it didn't work. My schema didn't contain the name field. I added the field and it worked. The reason I didn't discover this earlier was because apparently you can create new documents without the schema being complete.

@hallas hallas closed this as completed Aug 19, 2012
@phoebeli013
Copy link

I met the same problem. How do you fix it?

@hallas
Copy link
Author

hallas commented Sep 12, 2014

Long ago. Make sure that your schema has all the fields you are trying to find and modify. Other parts of Mongoose may still work without the field in the schema.

Maybe say some more about your problem and @aheckmann can help you out :)

@vkarpov15
Copy link
Collaborator

Agreed with @hallas . Make sure your schema contains all the fields you're trying to $set. If you can provide me with some isolated code that can repro this, I'll be happy to investigate :)

@jotave42
Copy link

jotave42 commented Sep 9, 2019

i was having the same issue. i fixed with a await before User.findByIdAndUpdate

@Automattic Automattic locked as resolved and limited conversation to collaborators Sep 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants