【问题标题】:Cannot insert/update after Azure Cosmos MongoDB migrationAzure Cosmos MongoDB 迁移后无法插入/更新
【发布时间】:2019-03-26 08:07:12
【问题描述】:

在将 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。这工作得很好,直到迁移之后。

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

【问题讨论】:

  • CosmosDB 不是 MongoDB。这是一个完全不同的产品,只是声称与 MongoDB 使用的有线协议及其驱动程序兼容。该错误与分片有关,MongoDB 如何处理此问题在Single Document Modification Operations in Sharded Collections 部分中有明确记录。 CosmosDB 处理这个问题的方式充其量是不可靠的,当然也不一致。
  • 数据迁移工具充其量看起来很狡猾。我可以解决这个问题的唯一方法是为每个集合定义分片键。我试图在迁移过程中将它们留空,这导致了错误。下次我可能会考虑使用 Linux VM 而不是 CosmosDB
  • 您是使用数据迁移工具还是使用其他迁移工具如docs.microsoft.com/en-us/azure/cosmos-db/mongodb-migrate进行迁移
  • 我确实使用了数据迁移工具

标签: mongodb azure azure-cosmosdb


【解决方案1】:

根据 cmets,您使用了 Data Migration Tool,它旨在迁移到 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。

【讨论】:

  • 好吧,我做了指南描述的确切过程。问题是,一旦迁移完成,它就设置了一个分片键(它不应该这样做)。顺便说一句,链接建议的解决方案没有什么特别之处,mongodb 也没有什么独特之处。
  • 问题在于数据迁移工具,正如注释所暗示的那样,使用 SQL API,而 Mongo API 帐户旨在与 Mongo Wire Protocol 工具和驱动程序一起使用。在 DMS 指南中,指定 Shard Key looks optional,您是否设置了它?正如我提到的,mongoimport 和 mongorestore 也应该是有效的。
猜你喜欢
  • 1970-01-01
  • 2019-08-05
  • 1970-01-01
  • 2021-06-16
  • 2018-08-01
  • 1970-01-01
  • 2021-07-27
  • 2019-10-08
  • 2019-07-01
相关资源
最近更新 更多