find() query on a model by non existing property returns whole collection
See original GitHub issueDo you want to request a feature or report a bug?
Bug
What is the current behavior?
find() query on a model by non existing property returns whole collection.
If the current behavior is a bug, please provide the steps to reproduce.
const templateSchema = mongoose.Schema({
title: { type: String, required: true },
}, {
timestamps: true,
versionKey: false,
})
const Template = mongoose.model('Template', templateSchema)
await Template.create({ title: 'some title' })
const queryResult = await Template.find({ test: 'test' })
console.log(queryResult)
// [{
// _id: new ObjectId("5adf374309ee5958d92c4133"),
// updatedAt: 2020-03-19T12:50:33.280Z,
// createdAt: 2020-04-24T13:55:15.779Z,
// title: 'some title',
// }]
If enable mongoose debug, next query to DB will be logged:
Mongoose: templates.find({}, { projection: {} })
What is the expected behavior?
- find() query on a model by non existing property returns empty array.
- Not sure here, query to DB should be templates.find({ test: ‘test’ }, { projection: {} }) or DB should not be queried at all?
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version. Node.js: v14.17.4 Mongo: v4.0.18 Mongoose: 6.0.6
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (2 by maintainers)
Top Results From Across the Web
Mongoosejs .find returning whole model instead of document
I am running on .find() query on an existing model. I have used this code in the past and have changed nothing but...
Read more >Project Fields to Return from Query — MongoDB Manual
If you do not specify a projection document, the db.collection.find() method returns all fields in the matching documents. The following example returns all ......
Read more >Mongoose v6.8.2: Query
Returns an asyncIterator for use with for/await/of loops This function only works for find() queries. You do not need to call this function...
Read more >2.5. Querying the database - MongoEngine Documentation
2.5.5. Default Document queries¶ · objects attribute on a document returns a QuerySet that doesn't filter the collection – it returns all objects....
Read more >Query examples | Objection.js
Find queries can be created by calling Model.query() and chaining query builder methods for the returned QueryBuilder instance. In addition to the examples...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
We added back the
strictQuery
option for 6.0.10. If this behavior is causing issues for you, you can add the below code before defining any models or schemas to opt out:I’m sorry for the inconvenience and confusion this issue caused.
I was pointed here and I agree this should not behave like it is now. It should reflect mongodb. It could easily lead to some security issues or just dumb errors. I want my schema to be validated on update/save only. Here is example that could lead to sec issues.
It will return random user from db as we made a mistake and added extra S in ‘users’;