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

db.find({somefield:{$exists:true}}) returns all docs, but no doc contains the somefield! #11807

Closed
creedofcool opened this issue May 14, 2022 · 6 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@creedofcool
Copy link

mongoos 6.3.3
mongodb 4.2
express 5.0 beta

why?

@Uzlopak Uzlopak added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label May 14, 2022
@creedofcool
Copy link
Author

not happen at mongose@5.13.14 and 4.x
so is it a bug?

@AbdelrahmanHafez
Copy link
Collaborator

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';
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:

$ node 11807.js
6.3.2
All assertions passed.

@creedofcool
Copy link
Author

creedofcool commented May 17, 2022

const mongoose = require("mongoose");
const { Schema } = mongoose;

const start = async () => {
  const mongodb_uri = "mongodb://localhost:38517/orm";
  await mongoose.connect(mongodb_uri);
  const userSchema = new Schema({
    name: { type: String, unique:true },
    age: Number,
  });
  const testUser = mongoose.model("testUser", userSchema);
  await testUser.insertMany([
    { name: "John" },
    { name: "Doe" },
    { name: "Jane", age: null },
  ]);

  const foundUsers = await testUser.find({ username: { $exists: true } });
  console.log(foundUsers);

  console.log(\`node version: ${process.version} && mongoose version: ${mongoose.version}\` );

  process.exit(1);
};

start();

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
Copy link
Collaborator

Uzlopak commented May 17, 2022

There is no field username in your Schema.

@AbdelrahmanHafez
Copy link
Collaborator

AbdelrahmanHafez commented 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 using strictQuery: false

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?

@creedofcool
Copy link
Author

Yes,
That's right, so awesome.
Thank you so much. @AbdelrahmanHafez

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

3 participants