【发布时间】:2021-06-18 22:08:00
【问题描述】:
我有产品架构,其中有一个字段 storeZones 对象定义为
...
storeZones: {
type: {
masterZone: {type: Schema.Types.ObjectId, model: 'Zone', index: 1 },
zone: { type: Schema.Types.ObjectId, model: 'Zone', index: 1 },
subZone: { type: Schema.Types.ObjectId, model: 'Zone', index: 1 },
},
default: {
masterZone: null,
zone: null,
subZone: null,
},
},
...
我正在计算特定 masterZone 中的产品。所以我的查询是
const condition = { 'storeZones.masterZone': masterZone };
console.log(condition); // { 'storeZones.masterZone': '60533e6a745d465ab6cb3fc9' }
const total = await Product.count(condition);
这将返回 0 个结果。 但是当我将确切的查询粘贴到 mongo shell 中时;确切地说是Robo3t。
db.products.find({'storeZones.masterZone': ObjectId('60533e6a745d465ab6cb3fc9') } )
它产生所需的输出。有人可以提供一些帮助吗?
【问题讨论】:
-
通过手动将猫鼬查询中的
masterZone从string转换为ObjectId来修复它。奇怪的是,我不得不手动执行此操作,因为 mongoose 自己执行此操作,因为我已将 masterZone 定义为ObjectId。
标签: javascript mongodb mongoose robo3t