我有三个集合,我想将数据从集合A移到由另一个数据库中的集合C过滤的B。
问题是MongoDb失败了,无论查询不能移动超过100个文档而失败,我认为我都需要一个优化的查询,我这样做:
db.getSiblingDB('OTHERDATABASE').C.find().forEach(function (d) {
db.getCollection('A').find({_id:d._id}).forEach(function (x) {
db.getCollection('B').insert(x);
})
});
在以下问题上,由于网络错误而失败,我使用以下PHP脚本解决了该问题:
$client=new MongoDB\Client(MONGODB);
$databaseab = $client->selectDatabase("DATABASAB");
$databasec = $client->selectDatabase("DATABASEC");
$A = $databaseab->selectCollection("A");
$B = $databaseab->selectCollection("B");
$C = $databasec->selectCollection("C");
$list = $C->distinct("_id");
do
{
$split = [];
$count = 0;
while (($line = array_shift($list)) AND $count++ <= 500)
{
$split[] = $line;
}
$result = $A->find([
'_id' => [
'$in' => $split
]
]);
foreach ($result as $document)
{
$B->findOneAndUpdate(
[
'_id' => $document['_id']
],
[
'$setOnInsert' => $document
],
[
'upsert' => TRUE
]
);
}
$A->deleteMany([
'_id' => [
'$in' => $split
]
]);
} while (count($list));