MongoDB:用于整合数组的功能
MongoDB: Function to Consolidate Arrays
我有一个大型数据集,其中的文档有时相互交叉引用,有时不相互引用。在我可以基于这些交叉引用进行mapreduce之前,我必须将交叉引用数组设置为对于交叉引用中的每个值都相同。
我在shell函数中使用它来合并这些数组:
function fixArray2() {
var counter = 0;
// I only want the xref for each field, I don't even want the id
var cursor = db.catalog.find({}, {xref: true, _id: false});
// I don't want to init this inside the loop, worried about memory leaks
var consolidatedArray = [];
while (cursor.hasNext()) {
var xref1 = cursor.next().xref;
// first pass: create a consolidated array when the cross references match
var limitedCursor1 = db.catalog.find({"name":{$in:xref1}});
while (limitedCursor1.hasNext()) {
var doc1 = limitedCursor1.next();
consolidatedArray = consolidatedArray.concat(doc1.xref);
}
consolidatedArray = consolidatedArray.unique();
// now that we have the consolidated array, reset the xref field of the object to it
for (var i=0; i<consolidatedArray.length; i++) {
db.catalog.update({name:consolidatedArray[i]},{$set:{xref: consolidatedArray}},false, true);
}
consolidatedArray.length = 0;
counter++;
if (counter % 1000 == 0) {
print("Processed " + counter + " documents.");
}
}
}
它可以工作,但是我必须经常运行它。有人能提出改进建议吗?
如果您在将文档写入集合时提前完成该工作,则可以避免在稍后执行该map-reduce操作。
因此,获取需要交叉引用的文档列表,并在插入时将它们与文档一起写入。根据需要进行更新,例如,当一个文档被删除或不再引用另一个文档时。
相关文章:
- 使用复杂数组时Jquery自动完成功能不起作用
- 数组长度 内部功能不同
- Javascript-具有Split功能的数组
- 需要使用此独特的数组功能进行即兴创作
- 如何使用上一个/下一个功能循环数组中的图像
- Jasmine spyOn使用伪造的返回数组测试功能
- 在具有拆分功能的数组中推送数组
- Javascript数组指向具有类似功能的变量名
- 在javascript中为点击功能制作数组
- D3.js键功能在简单的选择器/数组组合上运行两次
- 数组中的第一个视频始终是第一个或第二个在下一个视频功能上播放
- 在Casper重复功能之外无法访问数组
- 如何在选择菜单中实现数组功能
- jquery将URL存储到数组中,并使用.click()打开它们;带控件的功能
- Wp编辑器设置数组自动保存功能
- MongoDB:用于整合数组的功能
- 为什么数组推不工作在我的功能
- 从对象数组上的属性创建数组的功能方法
- 如何渲染异步数组的结果forEach '功能
- 对象的子数组的数组,如何不修改原数组的功能