-
-
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
Projection with '+' not including field (since v7.1.1) #13773
Comments
Something also happened in v7.2.1 with Run the following code with v7.2.1 to see that const mongoose = require("mongoose");
async function main() {
await mongoose.connect("mongodb://127.0.0.1:27017/test");
const kittySchema = new mongoose.Schema({
name: String,
age: {
select: false,
type: Number,
},
});
const Kitten = mongoose.model("Kitten", kittySchema);
const silence = new Kitten({ name: "Silence", age: 2 });
await silence.save();
const cursor = Kitten.find().select("+age").cursor();
for await (const kitten of cursor) {
console.log(kitten);
}
mongoose.disconnect();
}
main(); This outputs:
While running with version 7.2.0 gives:
|
Mongoose 7.4.5 does not have this issue. Unable to reproduce. const mongoose = require('mongoose');
async function main() {
await mongoose.connect('mongodb://127.0.0.1:27017/test');
const kittySchema = new mongoose.Schema({
name: String,
age: {
select: false,
type: Number
}
});
const Kitten = mongoose.model('Kitten', kittySchema);
const silence = new Kitten({ name: 'Silence', age: 2 });
await silence.save()
const updated = await Kitten.findByIdAndUpdate(silence._id, { age: 3 })
.select("+age")
console.log("UPDATED", updated) // <-- Returns _id, name and __v, but not age!
}
main()
|
This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days |
I have tested again, and yes, But not using The following code: const mongoose = require("mongoose");
async function main() {
await mongoose.connect("mongodb://127.0.0.1:27017/test");
const kittySchema = new mongoose.Schema({
name: String,
age: {
select: false,
type: Number,
},
});
const Kitten = mongoose.model("Kitten", kittySchema);
const silence = new Kitten({ name: "Silence", age: 2 });
await silence.save();
const cursor = Kitten.find().select("+age").cursor();
for await (const kitten of cursor) {
console.log(kitten);
}
mongoose.disconnect();
}
main(); Returns
But the expected result is
|
…de `select: false` fields with `+` projection using cursors Fix #13773
fix(QueryCursor): avoid double-applying schema paths so you can include `select: false` fields with `+` projection using cursors
Prerequisites
Mongoose version
7.1.1
Node.js version
16.x, 18.x
MongoDB server version
4.4
Typescript version (if applicable)
No response
Description
This issue is very similar to this: #13340
7.1.1 is the first version that is bad. I have tested that 7.1.0 works, and that the latest version (7.4.4) also fails.
I have a Schema where one sub-document is not selected by default (
select: false
in schema definition). I have queries that needs this sub-document and therefore select it explicitly with the "+" syntax like so:.select("+myField")
.The problem is that this field is not returned although being explicitly selected.
Note that one difference from #13340 is that now the other fields are included, just not the one that is selected. In #13340 only the _id was returned – no other fields.
Steps to Reproduce
This can be reproduced by running the following code with mongoose >= 7.1.1. See that age is not returned.
If you use mongoose 7.1.0, age is included in the response as expected.
Result from the console.log using mongoose 7.1.1:
Result from the console.log using mongoose 7.1.0:
Expected Behavior
When using
.select('+someField')
,someField
should be included in the response.The text was updated successfully, but these errors were encountered: