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

Outputting document with wrong _id when executing simplest query with strict = false #10934

Closed
upirr opened this issue Oct 26, 2021 · 3 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@upirr
Copy link

upirr commented Oct 26, 2021

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

What is the current behavior?
When invoking simplest query findOne({ _id: }) returns correct document but it has wrong id from inner property
when setting strict = false.

If the current behavior is a bug, please provide the steps to reproduce.

Sample document

{
    "_id" : ObjectId("5a4cbff7c454930b8b90ff0f"),
    "test" : {
        "_id" : ObjectId("5a4cd761c454930b8b90ff54")
    }
}
const mongoose = require('mongoose');
mongoose.set('strict', false);

const Test= mongoose.model('Test', new mongoose.Schema({}, { collection: 'test' }));

(async () => {
	await mongoose.connect('mongodb://127.0.0.1:27017/test');
	const item = await Test.findOne({ _id: new mongoose.Types.ObjectId('5a4cbff7c454930b8b90ff0f') });
	console.info(item._id.toString()); // will return 5a4cd761c454930b8b90ff54
	process.exit();
})();

What is the expected behavior?
_id of returned document is '5a4cbff7c454930b8b90ff0f'

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node: v14.17.4,
"mongodb": "4.1.3",
"mongoose": "6.0.12"

@IslandRhythms
Copy link
Collaborator

This reporduction script will not work because _ids are created based on a timestamp. Furthermore, there are no documents in your model and therefore no _ids to find.

@IslandRhythms IslandRhythms added the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label Oct 26, 2021
@upirr
Copy link
Author

upirr commented Oct 26, 2021

May be this will clear the picture. So, if there's a nested document ('someLegacyProp' in this case) that is not present in schema, then mongoose will replace root-level document id with that nested object id.

const mongoose = require('mongoose');
mongoose.set('strict', false);

const Test = mongoose.model('Test', new mongoose.Schema({}, { collection: 'test' }));

(async () => {
	await mongoose.connect('mongodb://127.0.0.1:27017/test');

	const unrelatedObjectId = new mongoose.Types.ObjectId();

	const { insertedId } = await mongoose.connection.collection('test').insertOne({
		someLegacyProp: {
			// some legacy property that is not in model but is de-facto present in doc
			_id: unrelatedObjectId,
			someOtherProp: true,
		},
	});

	const entity = await Test.findOne({ _id: insertedId });

	console.log(insertedId);
	console.log(unrelatedObjectId);
	console.log(entity._id); // entity._id will be equal to unrelatedObjectId

	process.exit();
})();

@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity labels Oct 26, 2021
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');
mongoose.set('strict', false);

const Test = mongoose.model('Test', new mongoose.Schema({}, { collection: 'test' }));

(async () => {
	await mongoose.connect('mongodb://127.0.0.1:27017/test');
	await mongoose.connection.dropDatabase();

	const unrelatedObjectId = new mongoose.Types.ObjectId();

	const { insertedId } = await mongoose.connection.collection('test').insertOne({
		someLegacyProp: {
			// some legacy property that is not in model but is de-facto present in doc
			_id: unrelatedObjectId,
			someOtherProp: true,
		},
	})

	console.log(await Test.find());
	const entity = await Test.findOne({ _id: insertedId });
	console.log(insertedId);
	console.log(unrelatedObjectId);
	console.log(entity._id); // entity._id will be equal to unrelatedObjectId


})();

@vkarpov15 vkarpov15 added this to the 6.0.15 milestone Oct 30, 2021
@vkarpov15 vkarpov15 modified the milestones: 6.0.16, 6.0.15 Dec 5, 2021
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

3 participants