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

$set doesn't update subdocument #3265

Closed
jeffwhelpley opened this issue Aug 13, 2015 · 2 comments
Closed

$set doesn't update subdocument #3265

jeffwhelpley opened this issue Aug 13, 2015 · 2 comments
Milestone

Comments

@jeffwhelpley
Copy link

When I do this in mongoose 4.1.2:

user.findOneAndUpdate({ _id: someid }, { $set: { field1: 'val1', field2: { subfield1: 'sub1', subfield2: 'sub2' }}});

field1 is updated, but field2 is not. When I downgrade to mongoose 4.0.6 this works fine.

@vkarpov15
Copy link
Collaborator

What does your schema look like?

@jeffwhelpley
Copy link
Author

The code example I gave is fake. Here is a real example:

var postSchema = {
  title: String,
  answers: [{
    details: String,
    stats: {
      votes: Number,
      count: Number
    }
  }]
};

// later on in code
post.findOneAndUpdate(
  { _id: somePostId, answers._id: someAnswerId }, 
  { $set: { 'answers.$': { details: 'blah', stats: { votes: 1, count: 3 }}}}
);

So, in this example in 4.1.2 details is correctly updated to blah, but the stats field is completely removed from the answer. Even if there was a value in there before, its like stats was $unset or something. In 4.0.6, the details and stats are set correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants