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
db.find({somefield:{$exists:true}}) returns all docs, but no doc contains the somefield! #11807
Comments
not happen at mongose@5.13.14 and 4.x |
Is it possible that the field has the value of null in your database? Because setting the value to 11807.js'use strict';
import mongoose from 'mongoose';
const { Schema } = mongoose;
import assert from 'node:assert';
await mongoose.connect('mongodb://localhost:27017/test');
await mongoose.connection.dropDatabase();
const userSchema = new Schema({
name: { type: String },
age: Number
});
const User = mongoose.model('User', userSchema);
await User.insertMany([{ name: 'John' }, { name: 'Doe' }, { name: 'Jane', age: null }]);
const foundUsers = await User.find({ age: { $exists: true } });
// Jane is found, because you can set a field as `null`, and counts as a field
assert.ok(foundUsers.length === 1);
console.log(mongoose.version);
console.log('All assertions passed.'); Output:
|
output: Thank you @AbdelrahmanHafez |
There is no field username in your Schema. |
@creedofcool const foundUsers = await testUser.find({ username: { $exists: true } }, null, { strictQuery: false }); You can also set this option globally: mongoose.set('strictQuery', false);
// or on a single schema level
schema.set('strictQuery', false); Does that solve your problem? |
Yes, |
mongoos 6.3.3
mongodb 4.2
express 5.0 beta
why?
The text was updated successfully, but these errors were encountered: