我正在尝试聚合来自Mongo集合的数据,以便通过制作稍后要使用的数据的大型json文件来为FreeCodeCamp生成一些统计信息.
我正在遇到标题中的错误.似乎没有很多关于这方面的信息,而这里的其他帖子也没有答案.我正在使用最新版本的MongoDB和驱动程序.
我怀疑可能有更好的方法来运行这个聚合,但它在我的集合的一个子集上运行良好.我的完整系列大约是7GB.
我正在运行脚本通过node aggScript.js > ~/Desktop/output.json
以下是相关代码:
MongoClient.connect(secrets.db, function(err, database) {
if (err) {
throw err;
}
database.collection('user').aggregate([
{
$match: {
'completedChallenges': {
$exists: true
}
}
},
{
$match: {
'completedChallenges': {
$ne: ''
}
}
},
{
$match: {
'completedChallenges': {
$ne: null
}
}
},
{
$group: {
'_id': 1, 'completedChallenges': {
$addToSet: '$completedChallenges'
}
}
}
], {
allowDiskUse: true
}, function(err, results) {
if (err) { throw err; }
var aggData = results.map(function(camper) {
return _.flatten(camper.completedChallenges.map(function(challenges) {
return challenges.map(function(challenge) {
return {
name: challenge.name,
completedDate: challenge.completedDate,
solution: challenge.solution
};
});
}), true);
});
console.log(JSON.stringify(aggData));
process.exit(0);
});
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4629 次 |
最近记录: |