gpt4 book ai didi

node.js - 基于名称的 MongoDB/Mongoose 连接

转载 作者:可可西里 更新时间:2023-11-01 09:38:31 24 4
gpt4 key购买 nike

我目前有一个产品架构,其中包含一个包含不同数量对象的属性数组。一个对象看起来像这样:

[{ property: 'Color', value: 'Black' }, { property: 'Length', value: '1 inch' }]

每个产品都有随机数量的属性。问题是我无法查找产品并使用其标识符获取其属性。

下面是产品架构。

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', language: 'US', name: 'Color' }

我想获取一个产品并根据其名称和语言加入并获取 categoryPropertyId。

Mongoose 可以吗?我可以使用 javascript 循环来完成,但更喜欢加入之类的东西。

我查看了 $lookup 和 populate,但我对 mongoose 不是很熟练。感谢您的帮助!

一个产品

{
_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’
}
]
}

最佳答案

您可以尝试以下聚合

基本上你需要先$unwind "properties" 数组与外国“名称”相匹配... let让你将字段从根文档(产品)保留到内部文档(属性),最后使用 $group您可以将拆分后的“属性”再次回滚到“属性”数组。

如果你有 mongodb 版本 3.6

db.product.aggregate([
{ "$unwind": "$properties" },
{ "$lookup": {
"from": Properties.collection.name,
"let": { "property": "$properties.property" },
"pipeline": [
{ "$match": { "$expr": { "$eq": [ "$name", "$$property" ] } } },
{ "$project": { "categoryPropertyId": 1 }}
],
"as": "properties.prop"
}},
{ "$unwind": "$properties.prop" },
{ "$addFields": {
"properties.categoryPropertyId": "$properties.prop.categoryPropertyId"
}},
{ "$project": { "properties.prop": 0 }},
{ "$group": {
"_id": "$_id",
"created_at": { "$first": "$created_at" },
"language": { "$first": "$language" },
"category": { "$first": "$category" },
"articlenumber": { "$first": "$articlenumber" },
"name": { "$first": "$name" },
"properties": { "$push": "$properties" }
}}
])

如果你的 mongodb 版本低于 3.6

db.product.aggregate([
{ "$unwind": "$properties" },
{ "$lookup": {
"from": Properties.collection.name,
"localField": "properties.property",
"foreignField": "name",
"as": "properties.prop"
}},
{ "$unwind": "$properties.prop" },
{ "$addFields": {
"properties.categoryPropertyId": "$properties.prop.categoryPropertyId"
}},
{ "$project": { "properties.prop": 0 }},
{ "$group": {
"_id": "$_id",
"created_at": { "$first": "$created_at" },
"language": { "$first": "$language" },
"category": { "$first": "$category" },
"articlenumber": { "$first": "$articlenumber" },
"name": { "$first": "$name" },
"properties": { "$push": "$properties" }
}}
])

关于node.js - 基于名称的 MongoDB/Mongoose 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51538456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com