首页 > 解决方案 > Azure Cosmos MongoDB 迁移后无法插入/更新

问题描述

在将 Cosmos Mongo DB 迁移到新的 Cosmos Mongo DB 后,我遇到了问题。在我成功迁移并尝试更新集合中的项目后,我收到此错误:

MongoError: query in command must target a single shard key

这是我第一次看到这个错误。读取数据没有问题,但更新不再起作用。

例如:

// 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");
})
};

这是上述的方案:

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},
}

在这里,我使用了与 ObjectID 不同的 ID。这工作得很好,直到迁移之后。

有没有办法禁用分片或任何其他方法来解决这个问题?

标签: mongodbazureazure-cosmosdb

解决方案


根据评论,您使用了数据迁移工具,该工具旨在迁移到 SQL API 帐户。

官方文档说:

数据迁移工具当前不支持将 Azure Cosmos DB 的 MongoDB API 作为源或目标。如果要将数据迁移入或迁移出 Azure Cosmos DB 中的集合,请参阅如何使用 Azure Cosmos DB 的 API for MongoDB 将 MongoDB 数据迁移到 Cosmos 数据库以获取说明。你仍然可以使用数据迁移工具将数据从 MongoDB 导出到 Azure Cosmos DB SQL API 集合,以便与 SQL API 一起使用。

执行此操作的正确方法是遵循建议的解决方案:https ://docs.microsoft.com/azure/dms/tutorial-mongodb-cosmos-db

另一种选择是使用 mongoimport 和 mongorestore。


推荐阅读