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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Breaking change in findOne(ObjectId) method from mongoose 5 to mongoose 6 #12325
Comments
You're only passing in the object id, you need to pass in the form of |
It is recommended that you use |
I don't quite follow your answer @IslandRhythms . I made it clear that I know this may be not the right way to use findOne, and will recommend to use findById(). |
Do you understand that we are talking about a find method which return an different document @IslandRhythms ? |
The most likely explanation as to why it returns a totally random document is because how const testSchema = new mongoose.Schema({
name: String
})
const Test = mongoose.model('Test', testSchema);
// mongoose interprets this as findOne({name: 'Test'})
await Test.findOne({ name: 'Test'})
// mongoose interprets this as findOne();
await Test.findOne({ nickName: 'test' }); So when you pass in the ObjectId with no key, it interprets it as |
@IslandRhythms this is quiet different. There is an option to forbid filtering with an unknown field, you can set strictQuery to throw. In your example, allowing user to find by nickName can be forbid. This is exactly what we do in my company. |
Hello @vkarpov15 , do you think this shouldn't be fix ? It looks like a regression same as #8268. |
Hello, any news pls 馃憤 @vkarpov15 |
This burned us badly as well. Shouldn't just return a random document if anything was passed. |
fix(query): treat `findOne(_id)` as equivalent to `findOne({ _id })`
Prerequisites
Mongoose version
6.5.2
Node.js version
14
MongoDB server version
4.8.1
Description
Hello 馃憢
The query
findOne(ObjectId(id)
is not working as expected anymore. Even more it return not the document we are looking for.I looked in Changelog, and Documation about migrating from 5 to 6, I don't see this documented.
The native driver of mongo handles this correctly and I think mongoose should handle this too.
When I looked a #8268, I understood that was a bug and was fixed on >5.7.
We had a critical bug because a completely different document were returned, please let me knows.
Thank you.
PS:
I understand this is not the best solution to find a document with an ObjectId given, we have findById for that.
But this should throw an error at least, or return a null value but never another document.
Steps to Reproduce
Expected Behavior
No response
The text was updated successfully, but these errors were encountered: