Monday, 17 April 2017

How to use Group by with find in mongoose?

I have collection named "listing" with fields such as metadata and status. metadata is a object containing user object inside and status is a string.

The structure looks like this,

{ "status": "Active", "metadata": { "user": { "urlProfile": "", "averageRating": 5, "reviewCount": 2, "userId": "1244324" } } }

Now the status field have values such as "Active" and "Inactive". I need to group by those status and filter by the userId. so i have a function inside the helper as follows,

 query: function (model, conditon, options) {
            console.log(conditon, options);
            return new Promise(function (resolve, reject) {
                options = options || {};

                model.find(conditon, {}, options).exec(function (error, data) {
                    if (error) {
                        reject(error);
                    }
                    resolve(data);
                })
            })
        }

Question is how can i pass the group by along with the user id and query the data needed. Right now my querying part looks like this,

return dbService.query(sellerModel, {
                    'metadata.user.userId': userIdRetrieved
                }, {});

how can i pass the group by condition? i looked for sample, did not find any solution till now.



via Sajeetharan

No comments:

Post a Comment