question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

find() query on a model by non existing property returns whole collection

See original GitHub issue

Do 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?

  1. find() query on a model by non existing property returns empty array.
  2. 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:closed
  • Created 2 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
vkarpov15commented, Oct 7, 2021

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:

mongoose.set('strictQuery', false);

I’m sorry for the inconvenience and confusion this issue caused.

5reactions
xoxoxocommented, Sep 24, 2021

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.

const userSchema = mongoose.Schema({
  userName: { type: String, required: true },
})
userSchema.findOne({
  usersName:  'someuser'
});

It will return random user from db as we made a mistake and added extra S in ‘users’;

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found