[英]Mongoose does not produce a result but mongo shell does
I have schema for products and it has a field storeZones object in it defined as我有产品架构,它有一个字段 storeZones object 定义为
...
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,
},
},
...
I am counting for products in a specific masterZone
.我正在计算特定masterZone
中的产品。 So my query is所以我的查询是
const condition = { 'storeZones.masterZone': masterZone };
console.log(condition); // { 'storeZones.masterZone': '60533e6a745d465ab6cb3fc9' }
const total = await Product.count(condition);
This returns 0 results.这将返回 0 个结果。 But when i paste the exact query in mongo shell;但是当我将确切的查询粘贴到 mongo shell; Robo3t to be exact.确切地说是Robo3t。
db.products.find({'storeZones.masterZone': ObjectId('60533e6a745d465ab6cb3fc9') } )
It produces the desired output.它产生所需的 output。 Can someone please provide some assistance?有人可以提供一些帮助吗?
Fixed it by converting the masterZone
from request to an ObjectId
.通过将masterZone
从 request 转换为ObjectId
来修复它。 Idk why i needed to do this, but that fixed it.不知道为什么我需要这样做,但这解决了它。 so...所以...
const m = mongoose.Types.ObjectId(masterZone);
const condition = { 'storeZones.masterZone': m };
console.log(condition); // { 'storeZones.masterZone': '60533e6a745d465ab6cb3fc9'}
const total = await Product.count(condition);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.