为什么猫鼬删除了我的部分查询

问题描述:

我在 mongoDB CLI 中有以下查询:

I have the following query which works in the mongoDB CLI:

db.organisations.updateOne({ _id: ObjectId("5f687da23a4dc9071baefd6d")}, {$pull: {courseIds: 
ObjectId("5f687b863a4dc9071baefd6c"), "groups.$[].courseIds": 
ObjectId("5f687b863a4dc9071baefd6c") }})

以下查询是我认为与猫鼬等效的查询:

And the following query which is what I believe to be the mongoose equivalent:

await Organisation.updateOne({_id: "5f687da23a4dc9071baefd6d"},
{$pull: {courseIds: "5f687b863a4dc9071baefd6c", "groups.$[].courseIds": 
"5f687b863a4dc9071baefd6c"}})

使用 mongoose 调试,我可以看到以下 mongoose 转换为的查询删除了我对数组中所有文档的拉操作的整个后半部分.这是为什么?

Using mongoose debugging I can see that the following query which mongoose translates to is removing the entire second half of my pull operation for all documents in the array. Why is this?

organisations.updateOne({ _id: ObjectId("5f687da23a4dc9071baefd6d") }, { '$pull': { 
courseIds: ObjectId("5f687b863a4dc9071baefd6c") } }, {})

事实证明我有一个大笨蛋.我的猫鼬模式不正确.

It turns out I have the big stupid. My mongoose schemas were incorrect.