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

.save() throws DocumentNotFoundError when writeConcern.w=0 #11300

Closed
ekorolevanyrun opened this issue Jan 31, 2022 · 0 comments
Closed

.save() throws DocumentNotFoundError when writeConcern.w=0 #11300

ekorolevanyrun opened this issue Jan 31, 2022 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@ekorolevanyrun
Copy link

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
When I use { writeConcern: { w: 0, j: false } } schema options I got the error DocumentNotFoundError: No document found for query "{ _id: new ObjectId("61f794106eb7091ff50bdc62") }" on model "X" after saving documents.

If the current behavior is a bug, please provide the steps to reproduce.
You have to run several mongodb instances, primary and several secondary databases. Then you have to define any model like:

const mongoose = require('mongoose');

const someSchema = new mongoose.Schema({
    version: {
        type: Number,
        default: 1
    },
    someField: String,
    anotherField: Number,
},{
    writeConcern: {
        w: 0,
        j: false
    }
});

const someModel = mongoose.model('some_model', someSchema);

and then after connection established

const newDoc = new someModel();
newDoc.save(function (err) {
    if (err) {
        // You got an DocumentNotFoundError here
         console.error(err);
});

What is the expected behavior?

Function .save executes without any error.

Since I set up writeConcern.w = 0 to the schema options, there is unnecessary block code https://github.com/Automattic/mongoose/blob/master/lib/model.js#L382#L421 which is not needed to be executed. get(options, 'w') returns undefined, despite the fact that options equals { writeConcern: { w: 0, j: false } }.

I assume that the problem is in https://github.com/Automattic/mongoose/blob/master/lib/helpers/schema/applyWriteConcern.js . 10, 13, 16 lines should be the same as 21, 24, 27.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node: v14.18.1
Mongoose: 6.1.8
MongoDB server version: 5.0.5

ekorolevanyrun added a commit to anyrun/mongoose that referenced this issue Jan 31, 2022
@vkarpov15 vkarpov15 added this to the 6.1.10 milestone Jan 31, 2022
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Jan 31, 2022
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Feb 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants