0
votes

I am running into a problem after having migrated a Cosmos Mongo DB to a new Cosmos Mongo DB. After I have succesfully migrated and I try to update an item in a collection i get this error:

MongoError: query in command must target a single shard key

This is the first time I see this error. Reading data is no problem, but updating does not work any longer.

For example:

// Update suit
exports.update_suit = function (req, res, next) { 
  Suit.updateOne({
    id: req.params.id,
}, {
    $set: req.body
}, function (err, suit) {
    if (err) return next(err);
    res.send("Suit has been updated");
})
};

This is the schame for the above:

let SuitSchema = new Schema({
type: {type: String},
size: {type: String},
shoeSize: {type: String},
id: {type: String, unique: true, required: true},
location: {type: ObjectId}, 
status: {type: String},
nextService: {type: Date},
lastService: {type: Date},
condition: {type: String},
assignedTo: {type: Object, default: {}},
comment: {type: String},
make: {type: String},
model: {type: String},
suitType: {type:ObjectId},
year:{type: String},
vessel:{type: String},
guestSuit: {type: Boolean, default: false},
decomissioningReason: {type: ObjectId},
checkOutComment: {type: String},
inUseTempComment: {type: String},
}

Here I used a different ID than the ObjectID. This worked just fine until after the migration.

Is there any way to disable sharding or any other way to fix this?

1
CosmosDB is not MongoDB. It's a completely different product that simply claims to be compatible with the wire protocol used by MongoDB and it's drivers. The error is related to sharding, and how MongoDB handles this is clearly documented in the section Single Document Modification Operations in Sharded Collections. How CosmosDB handles this is unreliable at best and certainly not consistent. - Neil Lunn
The data migration tool seems dodgy at best. The only way I could fix the issue was by defining the shard key for each collection. I tried to leave them empty in the migration process which resulted in errors.Next time I might consider using a Linux VM instead of CosmosDB - TietjeDK
Did you migrate using the Data Migration Tool or using other migration tools like docs.microsoft.com/en-us/azure/cosmos-db/mongodb-migrate - Matias Quaranta
I did use the Data Migration Tool - TietjeDK

1 Answers

0
votes

As per the comments, you used Data Migration Tool, which is engineered to migrate to SQL API accounts.

The official docs say:

The Data Migration tool doesn't currently support Azure Cosmos DB's API for MongoDB either as a source or as a target. If you want to migrate the data in or out of collections in Azure Cosmos DB, refer to How to migrate MongoDB data a Cosmos database with Azure Cosmos DB's API for MongoDB for instructions. You can still use the Data Migration tool to export data from MongoDB to Azure Cosmos DB SQL API collections for use with the SQL API.

The correct way to do this is following the suggested solution: https://docs.microsoft.com/azure/dms/tutorial-mongodb-cosmos-db

Another alternative would be to use mongoimport and mongorestore.