-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Wrong values after simple query on mongodb since 6.2.1 #11526
Comments
Exact same problem. Mongoose goes nuts when a nested property uses the same name as a top level property with |
Modify this script to reproduce your bug. const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
name: String,
details: {
person: {
name: String
}
}
}, {strict: false, strictQuery: false});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await Test.create({
name: 'Foo',
details: {
person: {
name: 'Bar'
}
}
});
console.log(await Test.findOne())
}
run(); |
@IslandRhythms here it is: const mongoose = require('mongoose');
const testSchema = new mongoose.Schema(
{
name: String,
},
{ strict: false }
);
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await Test.create({
name: 'Foo',
details: {
person: {
name: 'Bar',
},
},
});
const test = await Test.findOne({}).exec();
console.log(test.name); // <--------- prints Bar instead of Foo
}
run(); |
There is realy issue with mongoos >=2.6.1 There are two documents in collention. One returns wrong "name": The difference is "key" order on Mongo DB self in collection. If attribute is last one, what happends in your case @IslandRhythms then everything works as expected, but if in document attribute is before nested - it takes last one. |
I definitively can reproduce it if i create object on Mongoo DB manualy. So the Object looks like: {"_id":{"$oid":"62309a639c0470bef6c6febd"},"name":"Full Name","person":{"firstName":{"name":"first Name"},"lastName":{"name":"last Name"}}} After that: const test1 = await Test.findOne({}).exec();
console.log(test1);
const test2 = await Test.findOne({}).select('name').exec();
console.log(test2); Output is: ❯ node src/test.js
Mongoose: tests.findOne({}, { projection: {} })
{
_id: new ObjectId("62309a639c0470bef6c6febd"),
name: 'last Name',
person: {
firstName: { name: 'first Name' },
lastName: { name: 'last Name' }
}
}
Mongoose: tests.findOne({}, { projection: { name: 1 } })
{ _id: new ObjectId("62309a639c0470bef6c6febd"), name: 'Full Name' } |
Here is even better script to reproduce. There is not "name" filled on create. But we get it if query: const mongoose = require('mongoose');
const testSchema = new mongoose.Schema(
{
name: String
},
{ strict: false, strictQuery: false }
);
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await Test.create({
details: {
person: {
name: 'Bar'
}
}
});
console.log(await Test.findOne());
}
run(); Result:
|
@jeanbmar Your script does not reproduce it. const mongoose = require('mongoose');
const testSchema = new mongoose.Schema(
{
name: String
},
{ strict: false, strictQuery: false }
);
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await Test.create({
details: {
person: {
name: 'Bar'
}
}
});
console.log(await Test.findOne());
}
run(); |
Hallo,
i found some regression in 6.2.1 version. It did worked all times till including 6.2.0 version.
MongoDB Data:
Output from mongoose 6.2.1 and higher:
Output from mongoose 6.2.0 and lower:
Mongoose >= 6.2.1 breaks "name" attribute of root object.
Schema:
The text was updated successfully, but these errors were encountered: