You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is it possible that the field has the value of null in your database? Because setting the value to null/undefined is not the same as $unsetting it.
If not, can you please modify the script below to reproduce the issue you're facing?
11807.js
'use strict';importmongoosefrom'mongoose';const{ Schema }=mongoose;importassertfrom'node:assert';awaitmongoose.connect('mongodb://localhost:27017/test');awaitmongoose.connection.dropDatabase();constuserSchema=newSchema({name: {type: String},age: Number});constUser=mongoose.model('User',userSchema);awaitUser.insertMany([{name: 'John'},{name: 'Doe'},{name: 'Jane',age: null}]);constfoundUsers=awaitUser.find({age: {$exists: true}});// Jane is found, because you can set a field as `null`, and counts as a fieldassert.ok(foundUsers.length===1);console.log(mongoose.version);console.log('All assertions passed.');
Thank you @AbdelrahmanHafez
If change the search field to username which is not included in any doc, the result is all docs. I don't understand the logic.
@creedofcool
You do not have a username field in your schema, so mongoose removes it from the filter. You can change this behavior by using strictQuery: false
Activity
creedofcool commentedon May 15, 2022
not happen at mongose@5.13.14 and 4.x
so is it a bug?
AbdelrahmanHafez commentedon May 15, 2022
Is it possible that the field has the value of null in your database? Because setting the value to
null/undefined
is not the same as$unset
ting it.If not, can you please modify the script below to reproduce the issue you're facing?
11807.js
Output:
creedofcool commentedon May 17, 2022
output:
[ { _id: new ObjectId("6283441e619ff0529bcaf97d"), name: 'John', __v: 0 }, { _id: new ObjectId("6283441e619ff0529bcaf97e"), name: 'Doe', __v: 0 }, { _id: new ObjectId("6283441e619ff0529bcaf97f"), name: 'Jane', age: null, __v: 0 } ] node version: v16.2.0 && mongoose version: 6.3.2
Thank you @AbdelrahmanHafez
If change the search field to username which is not included in any doc, the result is all docs. I don't understand the logic.
Uzlopak commentedon May 17, 2022
There is no field username in your Schema.
AbdelrahmanHafez commentedon May 17, 2022
@creedofcool
You do not have a
username
field in your schema, so mongoose removes it from the filter. You can change this behavior by usingstrictQuery: false
You can also set this option globally:
Does that solve your problem?
creedofcool commentedon May 18, 2022
Yes,
That's right, so awesome.
Thank you so much. @AbdelrahmanHafez
strictQuery
false
by default again #11861