【发布时间】: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