How can i make "Group By" query on loopback with mongodb connector ?
How can i make "Group By" query on loopback module with mongodb connector ? I can not find any document which say about that problem ? Please help me. Thanks
@peebles Hi, I have tried your code but it returned the message no response from the server, below is the screenshot of it, the second screenshot is the console.log output, please help, thank you:


The code:
Message.groupBy = function( filter, cb ) {
let deviceCollection = Message.getDataSource().connector.collection( Message.modelName );
// OPTIONAL if ( ! filter.where ) return cb( new Error( 'missing a where clause in the filter' ) );
if ( ! filter.groupBy ) return cb( new Error( 'missing a groupBy clause in the filter' ) );
let pipeline = [];
if ( filter.where )
pipeline.push({ $match: filter.where });
pipeline.push({
$group: {
_id: '$' + filter.groupBy,
count: { $sum: 1 }
}
});
// rename the _id field to be the name of the groupBy field
let p = {
_id: 0,
count: 1
};
p[ filter.groupBy ] = "$_id";
pipeline.push({
$project: p
});
console.log( 'pipeline:', JSON.stringify( pipeline, null, 2 ) );
deviceCollection.aggregate( pipeline, cb );}
Message.remoteMethod('groupBy', { isStatic: true, description: 'Invoke a group by', accessType: 'WRITE', accepts: {arg: 'filter', type: 'object', description: '{"where:{...}, "groupBy": "field"}'}, returns: {arg:'data', type:['object'], root:true} });
And it also come with this error in the terminal:
TypeError: Converting circular structure to JSON
at JSON.stringify (<anonymous>)
Is "field" one of the properties of "messages"? What does the schema for "messages" look like?
the field is topic, the schema:
{ "topic": "string", "temperature": 0, "timestamp": "2018-05-08T03:46:05.869Z", "name": "string", "description": "string", "tags": "string", "last_activity": "2018-05-08T03:46:05.869Z", "last_temperature": 0, "id": "string", "messageId": "string" }
Then please try:
{“groupBy”: “topic”}
A
On May 7, 2018, at 9:12 PM, JohnHour89 [email protected] wrote:
the field is topic, the schema:
{ "topic": "string", "temperature": 0, "timestamp": "2018-05-08T03:46:05.869Z", "name": "string", "description": "string", "tags": "string", "last_activity": "2018-05-08T03:46:05.869Z", "last_temperature": 0, "id": "string", "messageId": "string" }
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.
I modified the pipeline to just hard code the field to group however still getting TypeError: Converting circular structure to JSON logging my pipeline: pipeline: [ { "$group": { "_id": "$STAFF", "count": { "$sum": 1 } } }, { "$project": { "_id": 0, "count": 1, "STAFF": "$_id" } } ]
When you say whats the schema look like I'm assuming the model json properties... it looks like: "STAFF": { "type": "string", "required": false },
How do I debug correcting the circular structure error?
Try removing your debug line:
console.log( 'pipeline:', JSON.stringify( pipeline, null, 2 ) );
Otherwise, I don't know. Could be we are on different versions of loopback-connector-mongo. When I was doing this work, I was using loopback 2.0.
removed the console.log, modified the aggregate pipeline and 'cb' part to give me some more granularity in debug. Dropping the second section of the aggregate function to -
function (err, records) { console.log(records); if (err) return cb(err); //if(!err) console.log(JSON.stringify(records, null, 2)); if (records.length == 1) cb(null, true); else cb(null, false); }
at least gave me the connection object details but I think your right in the lb2 to lb3 doing some prioritization/laziness in executing the query and thereby getting results.
FOUND IT!! - The bulk of the issue was coming from the filter parsing. Ultimately If I changed: Model.groupBy = function (filter, cb) { to Model.groupBy = function (cb) {
IT WORKED!! I got results as expected atleast.
Note this will screw up being able to customise the groupBy. But atleast we/I know where the whole issue is coming from.
For others attempting this with LB3 and need some examples:
Model.groupBy = function (filter, cb) {
let modelCollection = Model.getDataSource().connector.collection(Model.modelName);
let pipeline = [];
pipeline.push({
$group: {
_id: '$STAFF',
//_id: '$' + filter.whateverjsonfields,
count: { $sum: 1 }
}
});
// rename the _id field to be the name of the groupBy field
let p = {
_id: 0,
count: 1
};
p["STAFF"] = "$_id";
//p[ filter.groupBy ] = "$_id";
pipeline.push({
$project: p
});
//console.log('pipeline:', JSON.stringify(pipeline, null, 2));
modelCollection.aggregate(pipeline, cb);
}
Sale.remoteMethod(
'groupBy', {
accepts: {arg: 'filter', type: 'json'},
http: {
path: '/groupBy',
verb: 'get'
},
description: 'Invoke a group by',
returns: {
arg: 'data',
type: 'json'
}
});
In your examples above, you show
Model.groupBy = function (filter, cb)
but your explanation of the problem suggested you had to drop the "filter" argument, ie:
Model.groupBy = function (cb)
So, is your example for L3 folks to follow incorrect?
No final post works really well. the 'drop the filter' commentary had something to do with issues in how the 'remotemethod' json section was configured and how the call then receives args.
Just for further help I managed to not only get aggregate grouping working as follows:
Sale.groupBy = function (filter, cb) {
//console.log(filter.filterfield);
let saleCollection = Sale.getDataSource().connector.collection(Sale.modelName);
// OPTIONAL if ( ! filter.where ) return cb( new Error( 'missing a where clause in the filter' ) );
//if (!filter.groupBy) return cb(new Error('missing a groupBy clause in the filter'));
let pipeline = [];
pipeline.push({
"$redact": {
"$cond": {
"if": {
"$and": [
{ "$gt": ["$" + filter.filterfield, new Date(filter.start)] },
{ "$lt": ["$" + filter.filterfield, new Date(filter.end)] },
]
},
"then": "$$KEEP",
"else": "$$PRUNE",
}
}
});
pipeline.push(
{ $unwind: '$_id' }
);
pipeline.push(
{
$group: {
_id: { ["$" + filter.groupBy]: "$" + filter.filterfield },
'stdDeviation': { '$stdDevSamp': "$" + filter.amountfield },
'revenueOfSales': { '$sum': "$" + filter.amountfield },
'avgSaleSize': { '$avg': "$" + filter.amountfield },
'numberOfSales': { '$sum': 1 },
}
}
);
pipeline.push(
{ '$sort': { ["_id"]: 1 } },
)
//console.log('pipeline:', JSON.stringify(pipeline, null, 2));
saleCollection.aggregate(pipeline, cb);
}
Sale.remoteMethod(
'groupBy', {
accepts: { arg: 'filter', type: 'json', description: 'Likely in form of {"amountfield": "SALE_AMOUNT", "filterfield": "SALE_DATE", "start": "2014-01-01", "end": "2016-01-01","groupBy": "month"}' },
http: {
path: '/groupBy',
verb: 'get'
},
description: 'Invoke a GroupBy for Sales Figures',
returns: {
arg: 'data',
type: 'json'
}
});
But Also proper 'across collection' aggregates working as follows:
Model.aggro = function (filter, cb) {
console.log(JSON.stringify(filter, null, 2));
console.log(filter.followcollection);
console.log(filter.leadfield);
console.log(filter.followfield);
Model.getDataSource().connector.connect(function (err, db) {
let pipeline = [
{
'$lookup':
{
from: filter.followcollection,
'localField': filter.leadfield,
'foreignField': filter.followfield,
as: "sold_staff_details"
},
},
{
"$unwind":
{
"path": "$sold_staff_details",
"preserveNullAndEmptyArrays": true
}
},
{
"$redact":
{
"$cond":
{
"if":
{
"$and":
[{
"$gt": ["$sold_staff_details.SALE_DATE", new Date(filter.start)
]
},
{
"$lt": ["$sold_staff_details.SALE_DATE", new Date(filter.end)
]
},
]
},
"then": "$$KEEP",
"else": "$$PRUNE"
}
}
},
{
'$group': {
_id: "$"+filter.leadID,
'numberOfModels': { '$sum': 1 },
'revenueOfModels': { '$sum': "$sold_staff_details.AMOUNT" },
'avgModelSize': { '$avg': "$sold_staff_details.AMOUNT" },
'stdDeviation': { '$stdDevSamp': "$sold_staff_details.AMOUNT" },
'childModels': { '$push': "$sold_staff_details" },
}
},
{ '$sort': { 'revenueOfModels': -1 } },
{ '$limit': 1000 }
];
let aggroCollection = db.collection(filter.leadcollection);
//console.log('pipeline:', JSON.stringify(pipeline, null, 2));
aggroCollection.aggregate(pipeline, cb);
});
}
Model.remoteMethod(
'aggro', {
accepts: { arg: 'filter', type: 'json', description: 'Likely in form of {"leadcollection": "model2", "followcollection": "model1", "leadfield": "STA_INITIALS", "followfield": "STAFF", "leadID": "STA_NAME", "start": "2016-01-01", "end": "2016-01-31"}'
},
http: {
path: '/aggro',
verb: 'get'
},
description: 'Can I Invoke an !Aggrogate! for ANY Mongo table!',
returns: {
arg: 'data',
type: 'json'
}
});
Point for anyone just copying pasting.... The filter JSON that the aggregate and group queries use should really have a bunch of default and error handling added to the logic. Specifically things like 'limit' especially for non index'd mongo attrs will bring the server and HW to a severe slow down.
Paying close attention to the arg input 'types' between lb3 and lb2 pays but seems to be a black art. The documentation on 'object' vs 'json' vs null doesn't make any sense on the website for LB3. In the end 'json' worked for me.
Interestingly after upgrading to connector 3.9.2
I get the following error at this line in my code:
var planCollection = PLAN.getDataSource().connector.collection(PLAN.modelName);
Error: MongoDB connection is not established
base on @peebles code, I made some adjustments based on a comment on https://github.com/strongloop/loopback-connector-mongodb/issues/434#issuecomment-398557920 and finally got the result for lb3 and mongo version 3.2.0
Membercheckin.groupBy = function (filter, cb) {
let memberCheckinCollection = Membercheckin.getDataSource().connector.collection(Membercheckin.modelName);
if (!filter.groupBy) {
return cb(new Error('missing a groupBy clause in the filter'));
}
let pipeline = [];
if (filter.where) {
pipeline.push({
$match: filter.where
});
}
pipeline.push({
$group: {
_id: '$' + filter.groupBy,
count: {
$sum: 1
}
}
});
let p = {
_id: 0,
count: 1
};
p[filter.groupBy] = "$_id";
pipeline.push({
$project: p
});
// console.log('pipeline: ', JSON.stringify(pipeline, null ,2));
memberCheckinCollection.aggregate(pipeline, function (error, cursor) {
if (error) {
cb(true);
return;
}
let result = [];
const callbackFn = function () {
cb(null, result);
}
cursor.on('data', function (doc) {
// console.log('doc: ', doc);
result.push(doc);
});
cursor.once('end', callbackFn);
// cb(null, cursor.toArray());
});
}
Membercheckin.remoteMethod('groupBy', {
http: {
path: '/groupBy',
verb: 'get'
},
description: 'Invoke a group by',
accepts: {
arg: 'filter',
type: 'object',
description: '{"where":{...}, "groupBy": "field"}'
},
returns: {
arg: 'data',
type: ['object'],
root: true
}
});`
filter {"groupBy":"member_id"} result:
[
{
"count": 1,
"member_id": "234"
},
{
"count": 95,
"member_id": "00000"
},
{
"count": 1,
"member_id": "121"
},
{
"count": 4,
"member_id": "348"
},
{
"count": 1,
"member_id": "48"
},
{
"count": 2,
"member_id": "112"
},
{
"count": 1,
"member_id": "120"
}
]
Finally, I got resolve the issue with the toArray method
Products.getDataSource().connector.connect(function(err, db) { var collection = db.collection('Products'); collection.aggregate( [{ $match: { "productCode" : "WIN10-NoOS" } }] ).toArray(function(err,servicesData){ if(err){ }else{ cb(null,servicesData); } }); });