[SERVER-149] $exists operator Created: 12/Jul/09  Updated: 12/Jul/16  Resolved: 12/Aug/09

Status: Closed
Project: Core Server
Component/s: Usability
Affects Version/s: None
Fix Version/s: 0.9.8

Type: New Feature Priority: Minor - P4
Reporter: Sascha Matzke Assignee: Aaron Staple
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

Please add an operator named "$exists" which checks if a certain path exists in a document.

Example:

Syntax:

{ 'a.b.c.$exists' 1 }

=> returns all documents which have a nested document "b" with a key "c"



 Comments   
Comment by Eliot Horowitz (Inactive) [ 15/Aug/09 ]

Released in 0.9.8

Comment by Aaron Staple [ 12/Aug/09 ]

Implemented and tested in exists.js

The following syntax seemed the most logical and consistent with other query operators:
db.c.find( {a:{$exists:true}} );
db.c.find( {a:{$exists:false}} );

Comment by Aaron Staple [ 12/Aug/09 ]

Ok cool, will do

Comment by Eliot Horowitz (Inactive) [ 11/Aug/09 ]

Right - this operator is specifically needed because there are cases where we don't want null and missing values to be treated the same.

Comment by Aaron Staple [ 11/Aug/09 ]

My impression was that in the matcher we generally treated null values and missing values equivalently. For example, in the above, if you try to find

{a:null}

using your example, both {} and

{a:null}

are returned. This is why I made sure that {a:{$ne:null}} would only match

{a:1}

of the three documents (rather than the first and last as described above).

Since we treat null and missing the same in basic finds, just want to double check that we want to differentiate between the two in a new $exists operator.

Comment by Eliot Horowitz (Inactive) [ 11/Aug/09 ]

$exists should be ok if its null
so if you have
{}

{ a : null } { a : 1 }

if i say $exists a, then it should return the last 2, not the first and last

Comment by Aaron Staple [ 11/Aug/09 ]

I think you can get the desired behavior with $ne: null. For example:
t.find( {'a.b':{$ne:null}} )

I just fixed a bug in that prevented this from working correctly in some cases, but latest git source should work correctly. Please reopen the bug if you have any trouble.

Comment by Namlook [ 15/Jul/09 ]

I think $exists operator is a good idea but I disagree with the syntax.

I think somethink like this is more consistant with other operators :

{"$exists":{"foo.bar":1, "foo.spam":0}}

wich mean, all documents where foo.bar exists and where foo.spam is not present in the document.

Generated at Thu Feb 08 02:53:10 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.