How can i check one property exists in mongodb collection by loopback model methods ?
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/
Can you try?
{
where: {
myProp: {
$exist: true
}
}
}
+1 - exist, $exist, exists are not available
@dennisaj: thanks for checking
+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": []
}
]
Looks like this is planned #1009
@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.
Works for exists:true, but doesn't work for exists:false right?
// 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.
@bluestaralone is this issue resolved using the suggestion in the previous comments?
this issue is resolved. I tested this afternoon, and both true and false options return the desired output
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 Try this: modelName.find({ fields: { a: true || false } }, callback) It returns an array of instances where the property exists or not.
@favour-nerrise Isn't fields filter used to include or exclude some property?
@Cyperwu Yeah, in this case it returns an array of objects [{a:....}, {a:....}] whereby the value of a is undefined or populated.
@favour-nerrise that's a pretty cool hack but not a right way to fix this bug lol
@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.
@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 } }] } ] }
@favour-nerrise no... it didn't fix mine... Anyway, thanks!
@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
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.