进行查询Product.findOne({'variation.sku': req.params.productVariationSKU },{'_id': 0, 'variation.price' :1})
以查找产品变化的价格。
但是结果返回了所有产品变化的价格。需要帮助来找出问题所在!
以下是产品架构:
var ProductSchema = new Schema({
productId: {
type: Number,
required: [true, 'Product ID is required'],
unique: true
},
sku: String,
category: [String],
language: {
type: String,
lowercase: true,
maxlength: 2,
required: [true, 'Language code required'],
index: true,
unique: true
},
name: {
type: String,
required: [true, 'Product name is required']
},
descLong: String,
descShort: String,
mainImageURL: String,
galleryImageURL: [String],
variation: [{
sku: String,
option: [{
label: String,
value: String,
}],
inventory: [{
name: String,
quantity: Number,
country: String
}],
inStock: Boolean,
price: mongoose.Types.Decimal128
}],
attribute: [{
label: String,
value: [String]
}],
inStock: Boolean,
currencySymbol: String,
slug: String,
metaTitle: String,
metaDescription: String,
relatedAccessory: [Number],
htmlContentBefore: String,
htmlContentAfter: String
});
以下是邮递员的产品对象:
{
"category": [],
"galleryImageURL": [
"TEST.png"
],
"relatedAccessory": [],
"_id": "5feae4418d686300176dfbbd",
"productId": 1,
"language": "en",
"name": "TEST PRODUCT",
"descLong": "<p><strong>This is a long description</strong></p>",
"descShort": "Short Description",
"mainImageURL": "TEST.png",
"variation": [
{
"option": [
{
"_id": "5feae4418d686300176dfbbf",
"label": "Color",
"value": "Black"
}
],
"inventory": [],
"_id": "5feae4418d686300176dfbbe",
"sku": "P-1",
"inStock": true,
"price": "45"
},
{
"option": [
{
"_id": "5feae4418d686300176dfbc1",
"label": "Color",
"value": "White"
}
],
"inventory": [],
"_id": "5feae4418d686300176dfbc0",
"sku": "P-2",
"inStock": true,
"price": "45"
}
],
"attribute": [
{
"value": [
"Black",
"White"
],
"_id": "5feae4418d686300176dfbc2",
"label": "Color"
}
],
"currencySymbol": "£",
"slug": "test",
"metaTitle": "testmeta",
"metaDescription": "This is meta description",
"__v": 0
}
以下是API函数:
app.get('/API/Product/GetProductVariationPrice/:productVariationSKU', async(req, res) => {
try{
res.send(await Product.findOne({'variation.sku': req.params.productVariationSKU },{'_id': 0, variation :1}));
} catch (err) {
res.status(500).send(err);
}
});
邮递员返回的结果:
{
"variation": [
{
"price": "45"
},
{
"price": "45"
}
]
}
问题:我如何只获得变型SKU“ P-1”的“ 45”?结果,当前还返回变体“ P-2”的价格,这是不需要的。SKU应该基于传递给API的参数。
通常,当我们从对象数组投影特定字段时,它将返回对象数组,
从MongoDB 4.4开始,作为使find
投影与聚合$project
阶段保持一致的一部分,
$reduce
迭代variation
数组循环,检查条件是否sku
匹配,然后返回price
await Product.findOne(
{ "variation.sku": req.params.productVariationSKU },
{
"_id": 0,
variation: {
$reduce: {
input: "$variation",
initialValue: 0,
in: {
$cond: [
{ $eq: ["$$this.sku", req.params.productVariationSKU] },
"$$this.price",
"$$value"
]
}
}
}
})
MongoDB 4.4或以下版本请尝试aggregate()
,
let p = Product.aggregate();
p.match({ "variation.sku": req.params.productVariationSKU });
p.project({
_id: 0,
variation: {
$reduce: {
input: "$variation",
initialValue: 0,
in: {
$cond: [
{ $eq: ["$$this.sku", req.params.productVariationSKU] },
"$$this.price",
"$$value"
]
}
}
}
});
res.send(await p.exec());
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句