【发布时间】:2019-01-03 10:15:33
【问题描述】:
我目前有一个产品架构,其中包含一个包含不同数量对象的属性数组。一个对象如下所示:
[{属性:'颜色',值:'黑色'},{属性:'长度',值:'1英寸'}]
每个产品都有随机数量的属性。问题是我无法查找产品并使用其标识符获取其属性。
下面是产品架构。
const mongoose = require('mongoose');
let product = new mongoose.Schema({
id: { type: mongoose.Schema.Types.ObjectId },
created_at: { type: Date, default: new Date()},
language: { type: String, required: true },
category: { type: String },
articlenumber: { type: String },
name: { type: String },
properties: { type: [{}] }
});
module.exports = product;
我的属性集合中还有一个带有标识符的属性架构。
const mongoose = require('mongoose');
let property = new mongoose.Schema({
id: { type: mongoose.Schema.Types.ObjectId },
categoryPropertyId: { type: String },
language: { type: String },
name: { type: String, unique: true }
});
module.exports = property;
{ id:ObjectID,categoryPropertyId:'317',语言:'US',名称:'Color'}
我想获取产品并根据其名称和语言加入并获取 categoryPropertyId。
猫鼬可以做到这一点吗?我可以使用 javascript 循环来做到这一点,但更喜欢加入之类的东西。
我查看了 $lookup 并填充,但我对猫鼬并不熟练。任何帮助表示赞赏!
产品
{
_id: 5b3f18d93098293c4ab0ca4a,
created_at: ‘2018-07-06 09:22:45.377’,
language: ‘US’,
category: ‘Phones’,
articlenumber: ‘6700’,
name: ‘Sony Phone’
properties: [
{
property: ‘Version’,
helptext: ‘’,
value: ’8.0’
},
{
property: ‘Operating System’,
helptext: ‘’,
value: ‘Android’
}]
}
属性数据集
[
{
id: ‘5b3603a56a14e1072ba6f4ef’,
categoryPropertyId: ’429’,
language: ‘US’,
name: ‘Android’
},
{
id: ‘5b3603a56a14e1072ba6f4ef’,
categoryPropertyId: ’977’,
language: ‘US’,
name: ‘Version’
},
{
id: ‘5b3603a56a14e1072ba6f4ef’,
categoryPropertyId: ’1033’,
language: ‘US’,
name: ‘Weight’
}
]
预期输出
{
_id: 5b3f18d93098293c4ab0ca4a,
created_at: ‘2018-07-06 09:22:45.377’,
language: ‘US’,
category: ‘Phones’,
articlenumber: ‘6700’,
name: ‘Sony Phone’
properties: [
{
property: ‘Version’,
helptext: ‘’,
value: ’8.0’,
categoryPropertyId: ’977’
},
{
property: ‘Operating System’,
helptext: ‘’,
value: ‘Android’,
categoryPropertyId: ’429’
}
]
}
【问题讨论】:
-
嗨 Ben,你能发布一些示例数据吗?以及您期望的输出。
-
嗨,杰克,我在帖子中添加了一些示例数据;加上预期的输出。
-
检查答案是否能解决您的问题?
-
嗨,杰克,很抱歉迟到了回复。该代码运行良好,但我注意到它没有考虑语言。有什么办法还需要另一个字段来匹配? (我只是添加另一个相等匹配吗?)
-
您还想在这里完成什么?您只能查找一个字段。