ist it possible to pass custom functions into a mongoose query? At the moment i am selecting several documents and afterwards i filter them with a service function in nodejs. Is it somehow possible to pass this function into the query conditions so that the check is executed earlier? The service function just takes several attributes of each document and performs its check.
Problem is that limit and offset are not working anymore if i filter after the query, so i would like to filter inside of my query.
Regards, Tobias
EDIT:
At the moment i am doing it like this (simplified):
Model.find(query)
.skip(offset)
.limit(limit)
.sort({timestamp: -1})
.populate(populationOptions)
.exec().then((docs) => {
// Only entries on which the user currently has the permission to see it
return Promise.all(docs.map((d) => {
PermissionService.hasPermission(d.requester._id.toString(), d._id)
.then((isAllowed) => {
return isAllowed;
});
return isAllowed;
})).then((bits) => {
return docs.filter(() => {
return bits.shift();
});
});
})
But if the user does not have the permission on some entity, the limit is not reached, but more entities would be still available. This is wrong. So i tried to put "hasPermissions"-Check directly into the query. The closest i got was with usage of virtuals where i calculated a new field which just had true or false.
via Tobias Stangl
No comments:
Post a Comment