最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

猫鼬骨料,找到产品的祖先当产品匹配

IT培训 admin 10浏览 0评论

猫鼬骨料,找到产品的祖先当产品匹配

很抱歉的坏称号,想不出一个更好的。

我有了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"
}

当我搜索一个产品,我想包括在搜索结果中的所有variantsoptions,如:

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,但没有运气后尝试过很多事情。

我一定要做出一个新的findaggregate产品匹配后或有一种方法来获取一个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"
}

当我搜索一个产品,我想包括在搜索结果中的所有variantsoptions,如:

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,但没有运气后尝试过很多事情。

我一定要做出一个新的findaggregate产品匹配后或有一种方法来获取一个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()
发布评论

评论列表 (0)

  1. 暂无评论