$lookup 迭代特定字段

Posted

技术标签:

【中文标题】$lookup 迭代特定字段【英文标题】:$lookup to iterate specific Fields 【发布时间】:2021-01-24 12:04:23 【问题描述】:

我正在尝试在 addtocart 中执行 all 请求时加入产品中的特定字段。我不知道如何更新此要求的查找。下面我更新了我的产品集合并添加到购物车集合。谁能建议我如何做到这一点?

加入购物车收藏:

 add_to_cart_products: [
      
        product: ObjectId('5f059f8e0b4f3a5c41c6f54d'),
        product_quantity: 5,
        product_item: ObjectId('5f4dddaf8596c12de258df20'),
      ,
    ],
    add_to_cart_product_total: 5,
    add_to_cart_discount: 50,

产品系列:

  
    _id: ObjectId('5f059f8e0b4f3a5c41c6f54d'),
    product_name: 'La Gioiosa Prosecco',
    product_description: 'No Description',
    product_overview: 'No Overview',
    product_items: [
      
        product_item_number: '781239007465',
        product_price: 14.99,
        product_images: ['pro03655-781239007465-1.png'],
        product_item_is_active: true,
        _id: ObjectId('5f4dddaf8596c12de258f021'),
      ,
      
        product_item_number: '850651005110',
        product_price: 12.99,
        product_images: ['default.png'],
        product_item_is_active: true,
        _id: ObjectId('5f4dddaf8596c12de258df20'),
      ,
    ],
    product_created_date: ISODate('2020-07-08T10:29:05.892Z'),
    product_status_is_active: true,
  ,

在我的 AddToCart 架构查找中

    lookups: [
      
        from: 'shop_db_products',
        let:  productId: '$add_to_cart_products.product', purchaseQuantity: '$add_to_cart_products.product_quantity' ,
        pipeline: [
          
            $match:  $expr:  $in: ['$_id', '$$productId']  ,
          ,
          
            $lookup: 
              from: 'shop_db_products',
              localField: 'product_id',
              foreignField: '_id',
              as: 'product',
            ,
          ,
          
            $project: 
              product_id: '$$productId',
              product_purchase_quantity: '$$purchaseQuantity',
              product_name: true,
            ,
          ,
          
            $unwind: '$product_id',
          ,
          
            $unwind: '$product_purchase_quantity',
          ,
        ],
        as: 'add_to_cart_products',
        model: 'ProductModel',
      ,
    ],

当前结果:

     "add_to_cart_products": [
                
                    "product_name": "Avery Coconut Porter",
                    "product_id": "5f059f8e0b4f3a5c41c6f54d",
                    "product_purchase_quantity": 5
                
            ],
            "add_to_cart_product_total": 5,
            "add_to_cart_discount": 50,

预期结果:

     "add_to_cart_products": [
                
                    "product_name": "Avery Coconut Porter",
                    "product_id": "5f059f8e0b4f3a5c41c6f54d",
                    "product_item":[
                          "product_price": 12.99,
                           "product_images": ["default.png"],
                      ],
                    "product_purchase_quantity": 5
                
            ],
            "add_to_cart_product_total": 5,
            "add_to_cart_discount": 50,

【问题讨论】:

【参考方案1】:

你可以试试,

$unwind解构add_to_cart_products数组 $lookupshop_db_products 集合传递 let 中的必填字段 $matchproductId 相等条件 $project 显示必填字段,并从数组 product_items 中获取产品项,使用 $filter 匹配 product_item_id$reductproduct_item 获取特定字段 $unwind解构add_to_cart_products数组 $group通过_id获取特定字段并构造add_to_cart_products数组
db.add_to_cart.aggregate([
   $unwind: "$add_to_cart_products" ,
  
    $lookup: 
      from: "shop_db_products",
      let: 
        productId: "$add_to_cart_products.product",
        purchaseQuantity: "$add_to_cart_products.product_quantity",
        product_item_id: "$add_to_cart_products.product_item"
      ,
      pipeline: [
         $match:  $expr:  $eq: ["$_id", "$$productId"]   ,
        
          $project: 
            product_name: 1,
            product_id: "$_id",
            product_purchase_quantity: "$$purchaseQuantity",
            product_item: 
              $reduce: 
                input: 
                  $filter: 
                    input: "$product_items",
                    cond:  $eq: ["$$product_item_id", "$$this._id"] 
                  
                ,
                initialValue: ,
                in: 
                  product_price: "$$this.product_price",
                  product_images: "$$this.product_images"
                
              
            
          
        
      ],
      as: "add_to_cart_products"
    
  ,
   $unwind: "$add_to_cart_products" ,
  
    $group: 
      _id: "$_id",
      add_to_cart_discount:  $first: "$add_to_cart_discount" ,
      add_to_cart_product_total:  $first: "$add_to_cart_product_total" ,
      add_to_cart_products:  $push: "$add_to_cart_products" 
    
  
])

Playground

【讨论】:

以上是关于$lookup 迭代特定字段的主要内容,如果未能解决你的问题,请参考以下文章

1.16字段分隔符和迭代器

如何使用 mongodb 在对象内迭代数组

迭代时无意中将键添加到哈希

如何使用 Ruby 中的特定步骤迭代反向范围?

如何迭代无序映射特定键值?

如何从 azure devops rest api 获得 n 个工作项的交互