gpt4 book ai didi

删除重复项时出现 MongoDB 错误

转载 作者:可可西里 更新时间:2023-11-01 09:24:16 25 4
gpt4 key购买 nike

我的重复删除代码(在 MongoDB shell 中)是这样的:

db.<collection_name>.aggregate([
{
$group: {
_id: { <duplicated_keys>: "$<duplicated_keys>" },
dups: { $addToSet: "$_id" },
count: { $sum: 1 }
}
},
{
$match: {
count: { $gt: 1 }
}
}
], { allowDiskUse: true })
.forEach(function(doc) {
doc.dups.shift();
db.<collection_name>.remove({ _id: { $in: doc.dups } });
});

我遇到了这样的错误:

[thread1] Error: getMore command failed: {
"ok" : 0,
"errmsg" : "Cursor not found, cursor id: 144931661890",
"code" : 43
}

这个错误的原因是什么?我该如何解决这个问题?

更新

  • MongoDB 版本为 3.2
  • forEach之前,结果是:

    { "_id" : { <duplicated_keys>: <dupkey_values> }, "dups" : [ ObjectId("56f8e4d37a88ea2aa938414d"), ObjectId("56f63ab87a88ea141ca33856") ], "count" : 2 }

    如果我用 ObjectId("56f63ab87a88ea141ca33856") 找到,它就是重复的文档。

  • 数据量比较大(30+GB),会不会是这个问题?
  • 在运行查询时,有对同一集合的插入。

最佳答案

终于找到解决办法了。 MongoDB 中的游标是有生命周期的,默认是 10 分钟。一旦超过这个时间,shell就再也找不到下一个游标了。

为避免这种情况,请使用 noCursorTimeout() 设置光标的生命周期。例如:

db.<collection_name>.aggregate([
{
$group: {
_id: { <duplicated_keys>: "$<duplicated_keys>" },
dups: { $addToSet: "$_id" },
count: { $sum: 1 }
}
},
{
$match: {
count: { $gt: 1 }
}
},
{
$out: "tempCollection"
}
], { allowDiskUse: true });

db.tempCollection.find().noCursorTimeout().forEach(...);

或者使用更小的批量大小。例如:

db.<collection_name>.aggregate([
{
$group: {
_id: { <duplicated_keys>: "$<duplicated_keys>" },
dups: { $addToSet: "$_id" },
count: { $sum: 1 }
}
},
{
$match: {
count: { $gt: 1 }
}
},
{
$out: "tempCollection"
}
],
{
allowDiskUse: true,
cursor: { batchSize: 0 }
});

db.tempCollection.find().forEach(...);

关于删除重复项时出现 MongoDB 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36262385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com