-
Notifications
You must be signed in to change notification settings - Fork 483
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
Comments
@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. |
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 :) |
from https://docs.mongodb.com/manual/reference/operator/query/exists/ 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}}). |
It would seem that (probably @VladimirTechMan) added some similar operators : 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 To cover what @jkieboom and @deenfoxx requested, using the |
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. |
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.
The text was updated successfully, but these errors were encountered: