Skip to content

Support for querying if a field exists or not #285

Closed

Description

@jkieboom

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.

Activity

  1. added this to the 1.4.0 Brach milestone on Nov 27, 2015
  2. techfort commented on Nov 27, 2015

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

  3. jkieboom commented on Nov 27, 2015

    @jkieboom
    Author

    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 :)

  4. deenfoxx commented on May 31, 2016

    @deenfoxx

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

  5. obeliskos commented on Jun 2, 2016

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

  6. VladimirTechMan commented on Jun 2, 2016

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

  7. stale commented on Jul 13, 2018

    @stale

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions