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.