猫鼬骨料,找到产品的祖先当产品匹配
很抱歉的坏称号,想不出一个更好的。
我有了Product
的阵列是空的父ancestors
。
我也有有ancestors
产品,其中包括父_id
,叫variants
和产品ancestors
,其中包括父,并呼吁_id
变异options
。所有产品都在同一文件中,没有裁判。
父产品
{
productType: 'simple'
ancestors: [],
title: "Test Title",
_id: "1"
}
变型产品
{
productType: 'variant'
ancestors: ["1"],
title: "Test Variant",
_id: "2"
}
期权产品
{
productType: 'option'
ancestors: ["1", "2"],
title: "Test Variant",
_id: "3"
}
当我搜索一个产品,我想包括在搜索结果中的所有variants
和options
,如:
products: [{
_id: "1",
variants: [{...}],
options: [{...}]
},
...
]
搜索端点
Product.aggregate([
{$match: {productType: 'simple'}},
{$match: {$or: [{sku: {$regex: new RegExp(makeComp(value), 'i')}}, {title: {$regex: new RegExp(makeComp(value), 'i')}}]}},
{
$project: {
_id : 1,
title : 1,
productType: 1,
ancestors: 1
}
},
], (err, result) => {
console.log(err);
console.log(result);
});
我已在$project
,但没有运气后尝试过很多事情。
我一定要做出一个新的find
或aggregate
产品匹配后或有一种方法来获取一个aggregate
内的所有祖先?
你可以简单地做find
与$or
db.products.find({$expr : {
$or : [
{"_id":1},
{"productType" : "variant", "ancestors" : 1},
{"productType" : "option", "ancestors" : 1}
]
}})
同样采用聚集
db.products.aggregate([
{$match: {$expr : {
$or : [
{"_id":1},
{"productType" : "variant", "ancestors" : 1},
{"productType" : "option", "ancestors" : 1}
]
}}}
])
编辑-1
与$lookup
得到的结果
db.products.aggregate([
{$match: {"_id":"1"}},
{$lookup: {
from: "products",
let: {id : "$_id", productType: "$productType"},
pipeline:[{$match: {$expr: {$and: [{$in: ["$$id","$ancestors"]}, {$eq: ["$productType" ,"variant"]}]}}}],
as: "variants"
}},
{$lookup: {
from: "products",
let: {id : "$_id", productType: "$productType"},
pipeline:[{$match: {$expr: {$and: [{$in: ["$$id","$ancestors"]}, {$eq: ["$productType" ,"option"]}]}}}],
as: "options"
}}
]).pretty()
编辑-2
与$match
,$group
和$project
db.products.aggregate([
{$match: {$expr : {
$or : [
{"_id":1},
{"productType" : "variant", "ancestors" : 1},
{"productType" : "option", "ancestors" : 1}
]
}}},
{$group: {_id: "$productType", docs : {$push : "$$ROOT"}}},
{$group: {_id: null, docs : {$push : "$docs"}}},
{$project: {
simple: {$arrayElemAt:[{$filter: {input : "$docs", as: "d", cond : {$in: ["simple", "$$d.productType"]}}},0]},
variant: {$arrayElemAt:[{$filter: {input : "$docs", as: "d", cond : {$in: ["variant", "$$d.productType"]}}},0]},
option: {$arrayElemAt:[{$filter: {input : "$docs", as: "d", cond : {$in: ["option", "$$d.productType"]}}},0]}
}}
]).pretty()
猫鼬骨料,找到产品的祖先当产品匹配
很抱歉的坏称号,想不出一个更好的。
我有了Product
的阵列是空的父ancestors
。
我也有有ancestors
产品,其中包括父_id
,叫variants
和产品ancestors
,其中包括父,并呼吁_id
变异options
。所有产品都在同一文件中,没有裁判。
父产品
{
productType: 'simple'
ancestors: [],
title: "Test Title",
_id: "1"
}
变型产品
{
productType: 'variant'
ancestors: ["1"],
title: "Test Variant",
_id: "2"
}
期权产品
{
productType: 'option'
ancestors: ["1", "2"],
title: "Test Variant",
_id: "3"
}
当我搜索一个产品,我想包括在搜索结果中的所有variants
和options
,如:
products: [{
_id: "1",
variants: [{...}],
options: [{...}]
},
...
]
搜索端点
Product.aggregate([
{$match: {productType: 'simple'}},
{$match: {$or: [{sku: {$regex: new RegExp(makeComp(value), 'i')}}, {title: {$regex: new RegExp(makeComp(value), 'i')}}]}},
{
$project: {
_id : 1,
title : 1,
productType: 1,
ancestors: 1
}
},
], (err, result) => {
console.log(err);
console.log(result);
});
我已在$project
,但没有运气后尝试过很多事情。
我一定要做出一个新的find
或aggregate
产品匹配后或有一种方法来获取一个aggregate
内的所有祖先?
你可以简单地做find
与$or
db.products.find({$expr : {
$or : [
{"_id":1},
{"productType" : "variant", "ancestors" : 1},
{"productType" : "option", "ancestors" : 1}
]
}})
同样采用聚集
db.products.aggregate([
{$match: {$expr : {
$or : [
{"_id":1},
{"productType" : "variant", "ancestors" : 1},
{"productType" : "option", "ancestors" : 1}
]
}}}
])
编辑-1
与$lookup
得到的结果
db.products.aggregate([
{$match: {"_id":"1"}},
{$lookup: {
from: "products",
let: {id : "$_id", productType: "$productType"},
pipeline:[{$match: {$expr: {$and: [{$in: ["$$id","$ancestors"]}, {$eq: ["$productType" ,"variant"]}]}}}],
as: "variants"
}},
{$lookup: {
from: "products",
let: {id : "$_id", productType: "$productType"},
pipeline:[{$match: {$expr: {$and: [{$in: ["$$id","$ancestors"]}, {$eq: ["$productType" ,"option"]}]}}}],
as: "options"
}}
]).pretty()
编辑-2
与$match
,$group
和$project
db.products.aggregate([
{$match: {$expr : {
$or : [
{"_id":1},
{"productType" : "variant", "ancestors" : 1},
{"productType" : "option", "ancestors" : 1}
]
}}},
{$group: {_id: "$productType", docs : {$push : "$$ROOT"}}},
{$group: {_id: null, docs : {$push : "$docs"}}},
{$project: {
simple: {$arrayElemAt:[{$filter: {input : "$docs", as: "d", cond : {$in: ["simple", "$$d.productType"]}}},0]},
variant: {$arrayElemAt:[{$filter: {input : "$docs", as: "d", cond : {$in: ["variant", "$$d.productType"]}}},0]},
option: {$arrayElemAt:[{$filter: {input : "$docs", as: "d", cond : {$in: ["option", "$$d.productType"]}}},0]}
}}
]).pretty()