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

Wrong values after simple query on mongodb since 6.2.1 #11526

Closed
nazarkulyk opened this issue Mar 14, 2022 · 8 comments
Closed

Wrong values after simple query on mongodb since 6.2.1 #11526

nazarkulyk opened this issue Mar 14, 2022 · 8 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@nazarkulyk
Copy link

nazarkulyk commented Mar 14, 2022

Hallo,

i found some regression in 6.2.1 version. It did worked all times till including 6.2.0 version.

MongoDB Data:

{ _id: 60817c815a30530098dd0d6f
name : "Morgen Star New"
details: Object
  prospectorDetails: Object
    name: "Günter Lüschen 10"
  exhibitorDetails: Object
    name: "Günter Lüschen 20"
}

Output from mongoose 6.2.1 and higher:

{ _id: new ObjectId("60817c815a30530098dd0d6f"),
  name: 'Günter Lüschen 20',
  details: {
    prospectorDetails: { name: 'Günter Lüschen 10' },
    exhibitorDetails: { name: 'Günter Lüschen 20' }
  }
}

Output from mongoose 6.2.0 and lower:

{ _id: new ObjectId("60817c815a30530098dd0d6f"),
  name: 'Morgen Star New',
  details: {
    prospectorDetails: { name: 'Günter Lüschen 10' },
    exhibitorDetails: { name: 'Günter Lüschen 20' }
  }
}

Mongoose >= 6.2.1 breaks "name" attribute of root object.

Schema:

export const HorseSchema = new Schema<Horse>(
  {
    name: { type: SchemaTypes.String, required: false, index: true }
  },
  { strict: false, strictQuery: false }
);
@jeanbmar
Copy link

jeanbmar commented Mar 14, 2022

Exact same problem. Mongoose goes nuts when a nested property uses the same name as a top level property with strict: false.
Doesn't happen when doing toObject() afterward.
It's a critical breaking issue imo.

@IslandRhythms IslandRhythms added the can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. label Mar 14, 2022
@IslandRhythms
Copy link
Collaborator

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();

@jeanbmar
Copy link

jeanbmar commented Mar 15, 2022

@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();

@nazarkulyk
Copy link
Author

There is realy issue with mongoos >=2.6.1

There are two documents in collention. One returns wrong "name":

image

image

image

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.
See screenshots

@nazarkulyk
Copy link
Author

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' }

@nazarkulyk
Copy link
Author

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:

❯ node src/test.js
{
  _id: new ObjectId("62309efc4de374c11812fbbe"),
  details: { person: { name: 'Bar' } },
  name: 'Bar',
  __v: 0
}

@IslandRhythms
Copy link
Collaborator

@jeanbmar Your script does not reproduce it.
@nazarkulyk Your script reproduces 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();

@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Mar 15, 2022
@vkarpov15 vkarpov15 added this to the 6.2.8 milestone Mar 18, 2022
@vkarpov15
Copy link
Collaborator

This issue was introduced when we fixed #11309 with d7649f2. Sorry for the trouble - the fix will be in v6.2.8.

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

4 participants