What's the safest method for moving a bulk of documents from one collection to another? Efficiency is welcome, but being safe is more important.
I've tried this solution: https://stackoverflow.com/a/41483981/2226028 but it fails with this message: Failed to target upsert by query :: could not extract exact shard key
I've tried this solution:
var documentsToMove = db.source.find().limit(1000);
documentsToMove.forEach(function(doc) {
db.target.insert(
{documents:doc,
ordered: false,
writeConcern: {w:0}});
db.source.remove(doc); } );
... but many documents just get lost without notice, the increase / decrease ratio in both collections is like "many documents removed in source are not being inserted in target".
There is a unique index on target, and that is on a key which is also the sharding key. I've also tried a python program with bulk_write with the same result ("many documents removed in source are not being inserted in target"). Any advice is welcome.