MongodB $exists always return false?

This is my collections in MongodB named Sparks:

(index) _id              ticketnumber     userid     username 
0      "AzpJ8bDBNRWruSqKp"  "CHG_1234"    CHG123     scheung
1      "h3Kj8bBrjFfbQJGA"   "CHG_2345"    CHG123     scheung

Sparks.find( { "ticketnumber.CHG_1234":{ $exists: true } } )

Why is it always returning false even when there is such field? According to docs is:
https://docs.mongodb.org/manual/reference/operator/query/exists/

$exists checks if a field is present or not, e.g. if you want to query for all Widgets that have a color field (with any value at all, even null or empty string), then you could do: Widgets.find({color: {$exists: true}}). This doesn’t seem to fit with what you are trying to do in your example…

I’m not sure but if ticketnumber is your field, you’d check for ticketnumber… not for the value of ticketnumber.

Like the documentation says:

$exists matches the documents that contain the field

If you want to find a specific value for your field, you’d search for it more like this:

Sparks.find( { ticketnumber: "CHG_1234" } )

But this Stackoverflow says:

“When you’re not matching a complete object you need to use dot notation to use an operator against an embedded object.”:

cursor = fols.find({‘followers.123’:{’$exists’: True}})

and that’s I am using, dot notation, but still returning false?

Sparks = new Mongo.Collection(‘sparks’);
var Schemas = {};

Schemas.Spark = new SimpleSchema({
ticketnumber: {
type: String,
label: “TICKET_NUMBER”,
max: 10,
index: true,
unique: true
},
userid: {
type: String,
label: “USER_ID”,
max: 10,
},
username: {
type: String,
label: “ASSIGNED_TO”,
max: 50,
}),
Sparks.attachSchema(Schemas.Spark);

"CHG_1234" is a value of the ticketnumber field, not a field itself. Your query is returning nothing because there are no documents that have a "ticketnumber.CHG_1234" field.

There are documents that have a value of "CHG_1234" in the ticketnumber field. To find them, you would do something like:

Sparks.find({
  "ticketnumber": "CHG_1234"
});

You’re not dealing with subdocuments here, so don’t worry about dot notation.

Thanks Pete and @Batjko, but whether it exists or not the returned object seems to be identical, so how to differentiate between one that exists vs non-existent object? Though doing a .count() on the returned object provides 1 vs 0. But this is not using the $exists keyword as Pete mentioned? So why use $exists if this can be performance just by defining the key and matching value pair?

You would use $exists if you wanted to know if some document had any value for the field you’re querying on.

A query like this:

Sparks.find({
  ticketnumber: { $exists: true }
});

Will return all of the documents in your first post, because all of them have values for ticketnumber. If you had some other spark document that didn’t have a value in ticketnumber (undefined), it wouldn’t be returned by the $exists: true query, but it would be returned by an $exists: false query.

I hope that helps. $exists just isn’t the right tool for the job here.

1 Like

Thanks Pete this clears it up.

2 Likes