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

Support for querying if a field exists or not #285

Closed
jkieboom opened this Issue Nov 26, 2015 · 6 comments

Comments

Projects
None yet
5 participants
@jkieboom

jkieboom commented Nov 26, 2015

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.

@techfort techfort added this to the 1.4.0 Brach milestone Nov 27, 2015

@techfort

This comment has been minimized.

Show comment
Hide comment
@techfort

techfort Nov 27, 2015

Owner

@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.

Owner

techfort commented 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

This comment has been minimized.

Show comment
Hide comment
@jkieboom

jkieboom Nov 27, 2015

I did a quick-n-dirty local hack to test:

if (typeof root === 'undefined' || root === null || !root.hasOwnProperty(path)) {
    return fun(root && root[path], value);
}

This doesn't seem the best way to do it, but it does work for my case :)

jkieboom commented Nov 27, 2015

I did a quick-n-dirty local hack to test:

if (typeof root === 'undefined' || root === null || !root.hasOwnProperty(path)) {
    return fun(root && root[path], value);
}

This doesn't seem the best way to do it, but it does work for my case :)

@deenfoxx

This comment has been minimized.

Show comment
Hide comment
@deenfoxx

deenfoxx 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}}).

deenfoxx commented 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

This comment has been minimized.

Show comment
Hide comment
@obeliskos

obeliskos Jun 2, 2016

Collaborator

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.

Collaborator

obeliskos commented 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

This comment has been minimized.

Show comment
Hide comment
@VladimirTechMan

VladimirTechMan Jun 2, 2016

Collaborator

@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.)

Collaborator

VladimirTechMan commented 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 $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.)

@stale

This comment has been minimized.

Show comment
Hide comment
@stale

stale bot 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.

stale bot commented 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.

@stale stale bot added the wontfix label Jul 13, 2018

@stale stale bot closed this Jul 20, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment