Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs + Mongodb: find data after aggregation

I'm a new to Nodejs and MongoDB.
Here is a sample of my dataset:

{ 
  'name': ABC,
  'age':24,
  'gender':male,
  ...
}

Generally speaking, what I want to do is to aggregate data before using them to find different data clusters.
To be specific, I want to know how many people there are at different ages. Then, to find people(documents) at each age and store them.

Here is my code:

MongoClient.connect(url, function(err, db) {
    if(err) { 
        console.log('Unable to connect to the mongoDB server. Error:', err); 
    } else { 
        db.collection('test').aggregate(
        [
          { $group: { _id: "$age" , total: { $sum: 1 } } },
          { $sort: { total: -1 } } 
        ]).toArray(function(err, result) {
            assert.equal(err, null);
            age = [];
            for(var i in result) {
                age.push(result[i]['_id'])
            };
            ageNodes = {};
            for(var i in age) {
                 nodes = [];
                 var cursor = db.collection('test').find({'age':age[i]});
                 // query based on aggregated data
                 cursor.each(function(err,doc){
                    if(doc!=null){
                        nodes.push(doc);
                    } else {
                        console.log(age[i]);
                        ageNodes[age[i]] = nodes;
                    }
                })
            }
            res.json(ageNodes);
        });
    };
});

My expected JSON format:

{
  age:[different documents]
}

example:

{
  20:[{name:A,gender:male,...},{},...],
  30:[{name:B,gender:male,...},{},...],
  ...
}

However, what I got was an empty result, so I think maybe it was caused by the for loop.
I have no idea how to handle the asynchronous callback.

like image 384
Lixing Liang Avatar asked Feb 05 '23 00:02

Lixing Liang


1 Answers

You only need to run the following pipeline which uses $push to add the root document (represented by $$ROOT system variable in the pipeline) to an array per age group:

Using MongoDB 3.4.4 and newer:

MongoClient.connect(url, function(err, db) {
    if(err) { 
        console.log('Unable to connect to the mongoDB server. Error:', err); 
    } else { 
        db.collection('test').aggregate([
            { '$group': { 
                '_id': '$age', 
                'total': { '$sum': 1 }, 
                'docs': { '$push': '$$ROOT' }
            } },
            { '$sort': { 'total': -1 } },
            { '$group': {
                '_id': null,
                'data': {
                    '$push': {
                        'k': '$_id',
                        'v': '$docs'
                    }
                }
            } },
            { '$replaceRoot': {
                'newRoot': { '$arrayToObject': '$data' }
            } }    
        ]).toArray(function(err, results) {
            console.log(results);
            res.json(results);
        });
    };
});

Using MongoDB 3.2 and below:

MongoClient.connect(url, function(err, db) {
    if(err) { 
        console.log('Unable to connect to the mongoDB server. Error:', err); 
    } else { 
        db.collection('test').aggregate([
            { '$group': { 
                '_id': '$age', 
                'total': { '$sum': 1 }, 
                'docs': { '$push': '$$ROOT' }
            } },
            { '$sort': { 'total': -1 } } 
        ]).toArray(function(err, results) {
            console.log(results);
            var ageNodes = results.reduce(function(obj, doc) { 
                obj[doc._id] = doc.docs
                return obj;
            }, {});
            console.log(ageNodes);
            res.json(ageNodes);
        });
    };
});
like image 96
chridam Avatar answered Feb 08 '23 15:02

chridam