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
{{ message }}
Repository navigation
Support for querying if a field exists or not #285
I could not find out how to do this, but maybe it's already possible. I'd like to be able to query for the existence (or non-existence) of a field. Something similar to the mongodb $exists operator.
@jkieboom this does not exist but should not be difficult to implement. Performance wise though it may not be the fastest, as it scans every object for the existence of a property.
from https://docs.mongodb.com/manual/reference/operator/query/exists/
"...$exists matches the documents that contain the field, including documents where the field value is null. If <boolean> is false, the query returns only the documents that do not contain the field."
I'm just starting to use LokiJS and had this issue. To keep things simple, assume I have a "categories" collection which contains documents with or without a parent_id. Example: {{id: 1, title: "root level"}, {id: 2, title: "sub-level", parent_id: 1}, ...}.
I was hoping to query categories.find({'parent_id': null}). But that didn't work. So I tried categories.find({'parent_id': undefined}). THIS WORKS!
So... the equivalent of mongoDB's "$exists: false" is simply to look for values equal to undefined. Is this a proper solution?
P.S. I haven't really tested it with "null" values, but it appears the reverse is true for the equivalent of mongoDB's "$exists: true"... just use categories.find({'parent_id': {"$ne": undefined}}).
If the field exists on the property but is set to undefined it will be equally as 'undefined' as if the property did not exist on the object at all though.
@obeliskos Yep, those two are from one of my past commits, but they cover a different use-case to what @jkieboom and @deenfoxx are looking for, I believe. The $definedin and $undefinedin operators allow to efficiently check if the value of a property of external object passed as the query's paramter is (not) equal to undefined, where the property name is the value of the specific field in the document. That typically is useful to find the documents where the value of a specific field belongs to a certain set of values. Similar to the $in and $nin operators, but using an object instead of an array.
To cover what @jkieboom and @deenfoxx requested, using the $eq and $ne operators with undefined is the easiest for now, from my perspective. (BTW, it also should benefit from the indexed fields when that optimization can be applied by our current query mechanism.)
Reacted by obeliskos, Mathieu Lory and Mohamed CISSE
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Activity
techfort commentedon Nov 27, 2015
@jkieboom this does not exist but should not be difficult to implement. Performance wise though it may not be the fastest, as it scans every object for the existence of a property.
jkieboom commentedon Nov 27, 2015
I did a quick-n-dirty local hack to test:
This doesn't seem the best way to do it, but it does work for my case :)
deenfoxx commentedon May 31, 2016
from https://docs.mongodb.com/manual/reference/operator/query/exists/
"...$exists matches the documents that contain the field, including documents where the field value is null. If <boolean> is false, the query returns only the documents that do not contain the field."
I'm just starting to use LokiJS and had this issue. To keep things simple, assume I have a "categories" collection which contains documents with or without a parent_id. Example: {{id: 1, title: "root level"}, {id: 2, title: "sub-level", parent_id: 1}, ...}.
I was hoping to query categories.find({'parent_id': null}). But that didn't work. So I tried categories.find({'parent_id': undefined}). THIS WORKS!
So... the equivalent of mongoDB's "$exists: false" is simply to look for values equal to undefined. Is this a proper solution?
P.S. I haven't really tested it with "null" values, but it appears the reverse is true for the equivalent of mongoDB's "$exists: true"... just use categories.find({'parent_id': {"$ne": undefined}}).
obeliskos commentedon Jun 2, 2016
It would seem that (probably @VladimirTechMan) added some similar operators :
$definedin and $undefinedin
If the field exists on the property but is set to undefined it will be equally as 'undefined' as if the property did not exist on the object at all though.
VladimirTechMan commentedon Jun 2, 2016
@obeliskos Yep, those two are from one of my past commits, but they cover a different use-case to what @jkieboom and @deenfoxx are looking for, I believe. The
$definedinand$undefinedinoperators allow to efficiently check if the value of a property of external object passed as the query's paramter is (not) equal toundefined, where the property name is the value of the specific field in the document. That typically is useful to find the documents where the value of a specific field belongs to a certain set of values. Similar to the$inand$ninoperators, but using an object instead of an array.To cover what @jkieboom and @deenfoxx requested, using the
$eqand$neoperators withundefinedis the easiest for now, from my perspective. (BTW, it also should benefit from the indexed fields when that optimization can be applied by our current query mechanism.)stale commentedon Jul 13, 2018
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.