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

Nonexistent fields get omitted from query #11048

Closed
ahmedelshenawy25 opened this issue Dec 6, 2021 · 6 comments
Closed

Nonexistent fields get omitted from query #11048

ahmedelshenawy25 opened this issue Dec 6, 2021 · 6 comments

Comments

@ahmedelshenawy25
Copy link
Contributor

ahmedelshenawy25 commented Dec 6, 2021

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

What is the current behavior?
Nonexistent fields get omitted from query which yields unexpected results.

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

import mongoose from 'mongoose';
import assert from 'assert';

run().catch(console.error);

async function run () {
  mongoose.set('debug', true);
  await mongoose.connect('mongodb://localhost:27017/test');

  const userSchema = new mongoose.Schema({ name: String });
  const User = mongoose.model('User', userSchema);

  await User.create({ name: 'foo' });
  const user = await User.findOne({ wrongField: 'bar' });

  assert.strictEqual(user, null);
}

What is the expected behavior?
it shouldn't return any documents.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js: v16.13.0
Mongoose: v6.0.14
MongoDB: v5.0.4

@Kamikadze4GAME
Copy link
Contributor

Duplicate of #10763, #10778, #10932

@ahmedelshenawy25
Copy link
Contributor Author

I can understand removing nonexistent fields from create or update, but imagine making a typo or having an old field which got removed from schema but not from the rest of our code in update filter. This could have significant unexpected results.

@vkarpov15
Copy link
Collaborator

mongoose.set('strictQuery', false). We agree this change has had some unintended consequences and we're sorry for the inconvenience. Please use the workaround mongoose.set('strictQuery', false) for now if this causes you issues.

@florianwalther-private
Copy link

@vkarpov15 The option is not working for me (with Mongoose 6.2.9), no matter if I set it all the way at the beginning of my code, other after connecting mongoose.

@vkarpov15
Copy link
Collaborator

@florianwalther-private make sure you do mongoose.set('strictQuery', false) before calling mongoose.model() or connection.model()

@florianwalther-private
Copy link

Sorry, I forgot to update this. My mistake was that I called it after setting up my routes (which in turn had dependencies on the models). It's all working now, I just needed to call it early enough :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants