loopback icon indicating copy to clipboard operation
loopback copied to clipboard

How can i check one property exists in mongodb collection by loopback model methods ?

Open bluestaralone opened this issue 11 years ago • 31 comments

How can i check one property exists in mongodb collection by loopback model methods ? This is describle by document below: http://docs.mongodb.org/manual/reference/operator/query/exists/

bluestaralone avatar Dec 15 '14 04:12 bluestaralone

Can you try?

{
  where: {
    myProp: {
      $exist: true
    }
  }
}

raymondfeng avatar Feb 06 '15 00:02 raymondfeng

+1 - exist, $exist, exists are not available

dennisaj avatar Mar 13 '15 15:03 dennisaj

@dennisaj: thanks for checking

bluestaralone avatar Mar 16 '15 02:03 bluestaralone

+1 PLEASE!!!!

Today I needed to return the list of categories/subcategories...

{
  "name": "category",
  "plural": "categories",
  "base": "PersistedModel",
  "idInjection": true,
  "properties": {
    "name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "subcategories": {
      "type": "hasMany",
      "model": "category",
      "foreignKey": "parentId"
    }
  },
  "acls": [],
  "methods": []
}

I want to return only all the categories in the root of the response, all all their related subcategories as the embedded object in the overridden method "find"...

module.exports = function(Category) {
  Category.on('attached', function() {
    var overridden = Category.find;
    Category.find = function(filter, callback) {
      filter = filter || {};
      filter.include = 'subcategories';
      filter.where = {
        parentId: { "exist": false }
      };
      arguments[0] = filter;
      return overridden.apply(this, arguments);
    };
  });
};

Since the objects does not have the property, the query DOES NOT RETURN!!! Just like it is defined, MongoDB has this http://stackoverflow.com/questions/5719408/mongo-find-items-that-dont-have-a-certain-field

Without the wrong "where" above, I can get all the values, but the Subcategories are also returned in the root of the list :(

[
  {
    "name": "Category 1",
    "id": 1,
    "subcategories": [
      {
        "name": "Sub 1.1",
        "id": 3,
        "parentId": 1,
        "subcategories": []
      },
      {
        "name": "Sub 1.2",
        "id": 4,
        "parentId": 1,
        "subcategories": []
      }
    ]
  },
  {
    "name": "Category 2",
    "id": 2,
    "subcategories": []
  },
  {
    "name": "Sub 1.1",
    "id": 3,
    "parentId": 1,
    "subcategories": []
  },
  {
    "name": "Sub 1.2",
    "id": 4,
    "parentId": 1,
    "subcategories": []
  }
]

marcellodesales avatar Mar 23 '15 17:03 marcellodesales

Looks like this is planned #1009

ghost avatar Mar 23 '15 19:03 ghost

@dennisaj Spread a little fud. This feature actually already works. See my note on the related article #1009

exists is not explicitly available but the condition will be evaluated by automatically adding $ to in the mongodb connector.

jsheely avatar Mar 24 '15 15:03 jsheely

Works for exists:true, but doesn't work for exists:false right?

arealmaas avatar Mar 10 '16 17:03 arealmaas

// URL
// /api/<modelName>?filter[where][<propertyName>][exists]=<true|false>

modelName.find({
    where: {
        propertyName: {
            exists: true
        }
    }
}, callback)

loopback-connector-mongodb append $ to any property which is not define in the loopback-connector-mongodb. So it will the $exists: true to the mongo database.

kashifeqbal avatar Sep 08 '16 10:09 kashifeqbal

@bluestaralone is this issue resolved using the suggestion in the previous comments?

loay avatar Mar 14 '17 17:03 loay

this issue is resolved. I tested this afternoon, and both true and false options return the desired output

jdhrivas avatar Mar 30 '17 22:03 jdhrivas

If I declare a property "a" in model definition json file, when i query modelName.find({ where: { a: { exists: true | false } } }, callback) neither true nor false will return any result

Cyperwu avatar Jul 12 '17 09:07 Cyperwu

@Cyperwu Try this: modelName.find({ fields: { a: true || false } }, callback) It returns an array of instances where the property exists or not.

favour-nerrise avatar Jul 12 '17 14:07 favour-nerrise

@favour-nerrise Isn't fields filter used to include or exclude some property?

Cyperwu avatar Jul 13 '17 06:07 Cyperwu

@Cyperwu Yeah, in this case it returns an array of objects [{a:....}, {a:....}] whereby the value of a is undefined or populated.

favour-nerrise avatar Jul 13 '17 11:07 favour-nerrise

@favour-nerrise that's a pretty cool hack but not a right way to fix this bug lol

Cyperwu avatar Jul 14 '17 01:07 Cyperwu

@Cyperwu True; I just suggested a temporary workaround since I faced a similar issue. Also, from what I understand form the comments above, this is in relation to the mongodb connector so you could check if your model is attached to the ds accordingly. That fixed mine.

favour-nerrise avatar Jul 14 '17 13:07 favour-nerrise

@Cyperwu I may be able to help you, can you describe what you are trying to accomplish? In my case I implemented soft deletes, by adding a new property to the model call "archived" with the value of true. Then I overwrote the "find" method by modifying the query with this:

query.where = { and: [ query.where, { or: [{[o.archived]: false}, {[o.archived]: null}, { [o.archived]: { exists: false } }] } ] }

jdhrivas avatar Jul 14 '17 15:07 jdhrivas

@favour-nerrise no... it didn't fix mine... Anyway, thanks!

Cyperwu avatar Jul 17 '17 01:07 Cyperwu

@jdhrivas I tried to find data without certain key(s) but if I defined that key in model definition JSON file, then the exists: true | false filter will return me nothing

Cyperwu avatar Jul 17 '17 01:07 Cyperwu

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] avatar Sep 15 '17 01:09 stale[bot]