-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Filter on __v missing after push() to nested array #11108
Comments
const mongoose = require('mongoose');
mongoose.set('debug', true)
const PostModel = mongoose.model(
'Post',
new mongoose.Schema({
comments: [{likedBy: [String]}],
})
);
// ...
async function run() {
await mongoose.connect('mongodb://localhost:27017/', {useNewUrlParser: true,
useUnifiedTopology: true,});
await mongoose.connection.dropDatabase();
const entry = await PostModel.create({
comments: [{likedBy: ['Friends', 'Family']}]
});
console.log(entry, entry.comments[0].likedBy)
const post = await PostModel.findById(entry._id).exec();
const comment = post.comments[0];
comment.likedBy.push('Some User');
await post.save();
console.log(post, post.comments[0].likedBy);
}
run(); |
This is not strictly related to the bug on this issue, but I'd also suggest we fix the code block in question to more safely assign to Here:
And here:
|
This is expected behavior, because If you want to be more defensive in concurrency, you can enable optimistic concurrency. |
@vkarpov15 - I'm aware that The issue is that the
If the This is the exact reason that |
@vkarpov15 - here is a proposed fix according to my comment above. |
@lee-alexander at first glance that fix looks good. Feel free to put in a PR 👍 |
Hey @vkarpov15 - I think 2d6bbf9 introduces a new bug / regression. Now any subpath of array will only set Happy to follow up with a PR next week. |
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
Calling
push()
on an array field on an object inside an array does not add a query filter on__v
as expected. This is important for concurrency because the indicies of the top-level array may have changed. Setting non-array fields on the nested object correctly adds a filter on__v
.If the current behavior is a bug, please provide the steps to reproduce.
Code:
Output:
What is the expected behavior?
This incorrect behavior is shared by
$push
,$addToSet
,$pullAll
, and$pull
. We can see that these exit early without checking to see if we are operating on a subpath of array (final else-if)model.js
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js: 14.7.1
MongoDB: 4.2.1
Mongoose: 6.1.2
The text was updated successfully, but these errors were encountered: