问题:如何知道聚合+ $ sort + $输出何时完成?
问题:我创建了新的集合“tmp332”。然后我使用aggregate + $ sort + $ output来创建新的集合“tmp332sorted”。如果我在代码示例中删除原始“tmp332”,“tmp332sorted”将只有一个文档。
为什么临时收藏而不只是使用聚合分组?我试图避免BufBuilder 64MB限制的问题。
Windows 8.1 Pro上的MongoDB 3.0.2本地安装(与2.6相同)
*Collection "cars" (2,5M documents with 19 fields each):*
..
{
"_id" : ObjectId("55115258eddc7b9bf363d323"),
..
"postcode" : "332",
"co2" : 190,
"km" : 229575,
..
}
..
Collection "carpostcodes" (848 documents):
..
{
"_id" : ObjectId("554654ecaabf97a2674f081d"),
"postcode" : "332"
}
..
Collection "tmp332km" and "tmp332kmsorted":
..
{
"_id" : ObjectId("5546b4a2b172d8982e2a7be7"),
"co2" : 190
}
..
var fields = [.. ,"co2","km", ..];
var groupCarDetails = function(document) {
for (i = 0; i < fields.length; i++) {
var emit_object = {};
var sort_object = {};
var colName = "tmp" + document.postcode + fields[i];
emit_object[fields[i]] = document[fields[i]]
sort_object[fields[i]] = 1;
db[colName].insert(emit_object);
db[colName].aggregate(
[
{ $sort : sort_object },
{ $out: colName + "sorted"}
]
)
db[colName].drop();
}
}
//skip and limit are just for testing purposes
db.carspostcodes.find().skip(100).limit( 2 ).forEach(function(obj){
db.cars.find({postcode: obj.postcode}).forEach(groupCarDetails);
});
我还尝试为db [colName] .drop();创建自己的for循环。只有单独运行js文件似乎工作。如果在相同的js文件中,以下代码没有帮助。
var deleteTmpCollections = function(document) {
for (i = 0; i < fields.length; i++) {
var colName = "tmp" + document.postcode + fields[i];
db[colName].drop();
}
}
db.carspostcodes.find().skip(100).limit( 2 ).forEach(function(obj){
db.cars.find({postcode: obj.postcode}).forEach(deleteTmpCollections);
});