如何在loopback远程方法中使用聚合分组

时间:2016-05-11 15:37:14

标签: node.js mongodb loopbackjs

我想在以下远程方法中使用group by aggregation。 我想在以下远程方法中使用group by aggregation。 我想在以下远程方法中使用group by aggregation。 我想在以下远程方法中使用group by aggregation。

// To fetch all surveys created by a particular user including other required details
    Survey.createdBy = function(id, cb) {
        Survey.find({
            where : {createdBy: id},
            include: {
                relation: 'group'
            }
        },
        function(err, surveys) { 
            if (err) cb(new Error('Something went wrong!'));
            cb(null, surveys);
        });
    };
    Survey.remoteMethod(
        'createdBy',
        {
            accepts: {arg: 'id', type: 'string', required: true},
            http: {path: '/createdBy/:id', verb: 'get'},
            returns: {type: 'object', root: true}
        }
    );

以上远程方法返回的结果如下。

[
  {
    "title": "Tell me about yourself",
    "publish": "yes",
    "id": "56a9011fe14fea482a6b0e17",
    "groupId": "56a63013b18000a16f41963e",
    "createdBy": "56a6384cc578273f73d594f3",
    "created": "2016-05-11T14:58:01.284Z",
    "modified": "2016-05-11T14:58:01.284Z",
    "group": {
      "name": "group 1 by user 1",
      "description": "g1",
      "id": "56a63013b18000a16f41963e",
      "imageId": "566e662949a2f16d31324f79",
      "createdBy": "56a6384cc578273f73d594f3",
      "created": "2016-01-25T14:24:19.221Z",
      "modified": "2016-01-25T14:24:19.221Z"
    }
  },
  {
    "title": "Third demo survey",
    "publish": "yes",
    "id": "56c31cc93e76fcaa41f93d7b",
    "groupId": "56a634e0b18000a16f41963f",
    "createdBy": "56a6384cc578273f73d594f3",
    "created": "2016-05-11T14:58:01.285Z",
    "modified": "2016-05-11T14:58:01.285Z",
    "group": {
      "name": "group 2 by user 1",
      "description": "g1",
      "id": "56a634e0b18000a16f41963f",
      "imageId": "566e662949a2f16d31324f79",
      "createdBy": "56a6384cc578273f73d594f3",
      "created": "2016-01-25T14:44:48.589Z",
      "modified": "2016-01-25T14:44:48.589Z"
    }
  },
  {
    "title": "Tell me about yourself",
    "publish": "no",
    "id": "56ef99e1d103db6969f40165",
    "groupId": "56a63013b18000a16f41963e",
    "createdBy": "56a6384cc578273f73d594f3",
    "created": "2016-03-21T06:51:13.746Z",
    "modified": "2016-03-21T06:51:13.745Z",
    "group": {
      "name": "group 1 by user 1",
      "description": "g1",
      "id": "56a63013b18000a16f41963e",
      "imageId": "566e662949a2f16d31324f79",
      "createdBy": "56a6384cc578273f73d594f3",
      "created": "2016-01-25T14:24:19.221Z",
      "modified": "2016-01-25T14:24:19.221Z"
    }
  },
  {
    "title": "Tell me about yourself",
    "publish": "no",
    "id": "56ef9a52710e3ad46bcfcbc8",
    "groupId": "56a63013b18000a16f41963e",
    "createdBy": "56a6384cc578273f73d594f3",
    "created": "2016-03-21T06:53:06.661Z",
    "modified": "2016-03-21T06:53:06.661Z",
    "group": {
      "name": "group 1 by user 1",
      "description": "g1",
      "id": "56a63013b18000a16f41963e",
      "imageId": "566e662949a2f16d31324f79",
      "createdBy": "56a6384cc578273f73d594f3",
      "created": "2016-01-25T14:24:19.221Z",
      "modified": "2016-01-25T14:24:19.221Z"
    }
  }
]

请帮我解释如何按groupId对结果进行分组。

0 个答案:

没有答案