Mongodb 在mongoose中添加查找后,在$project中添加剩余的文件

Mongodb 在mongoose中添加查找后,在$project中添加剩余的文件,mongodb,mongoose,mongodb-query,mongoose-schema,Mongodb,Mongoose,Mongodb Query,Mongoose Schema,我正在从一个_id-in-Order模式中进行$lookup,它的工作与预期的一样。但是在$project中如何添加剩余的键。我在下面添加了我的代码 产品系列: { "_id": "54759eb3c090d83494e2d804", "product_name": "sample product", "image": "default.png", "price&qu

我正在从一个_id-in-Order模式中进行$lookup,它的工作与预期的一样。但是在
$project
中如何添加剩余的键。我在下面添加了我的代码

产品系列:

{
"_id": "54759eb3c090d83494e2d804",
"product_name": "sample product",
"image": "default.png",
"price": 55,
"discount": 5,
}
{
    "user_name": "sample1",
    "product_list":[
       "product_id":{
          "product_name": "sample product"
        }
     ]
}
   {
      "user_name": "sample1",
       "product_list":[
           {
               "product_id": {
                    "product_name": "sample product"
                }
               "quantity": 5
           }
         ]
   }
订单列表集合

   {
        "user_name": "sample1",
        "product_list":[
        {
          "product_id": "54759eb3c090d83494e2d804"
          "quantity": 5
        }
        ]
    }
查找

[
      {
        from: 'product',
        localField: 'product_list.product_id',
        foreignField: '_id',
        as: 'product_list.product_id',
        model: 'ProductModel',
      },
    ],
$Project

{
      user_name: true,
      product_list: {
        $map: {
          input: '$product_list.product_id',
          as: 'product',
          in: {
            product_name: '$$product.product_name',
          },
        },
      },
    }
当前结果:

{
"_id": "54759eb3c090d83494e2d804",
"product_name": "sample product",
"image": "default.png",
"price": 55,
"discount": 5,
}
{
    "user_name": "sample1",
    "product_list":[
       "product_id":{
          "product_name": "sample product"
        }
     ]
}
   {
      "user_name": "sample1",
       "product_list":[
           {
               "product_id": {
                    "product_name": "sample product"
                }
               "quantity": 5
           }
         ]
   }
在此当前结果中,数量字段丢失。如何添加
$project
?。预期结果如下所示

预期结果:

{
"_id": "54759eb3c090d83494e2d804",
"product_name": "sample product",
"image": "default.png",
"price": 55,
"discount": 5,
}
{
    "user_name": "sample1",
    "product_list":[
       "product_id":{
          "product_name": "sample product"
        }
     ]
}
   {
      "user_name": "sample1",
       "product_list":[
           {
               "product_id": {
                    "product_name": "sample product"
                }
               "quantity": 5
           }
         ]
   }

您需要在
$lookup
之前执行
$unwind
,因为它不会直接在数组字段中工作,这里您不需要$project中的$map

  • 产品列表
    解构数组
  • ,这将允许使用管道内部查找,此处
    $project
    到必填字段
  • $unwind
    使用路径
    产品列表。产品id
    ,因为您需要它作为对象
  • 通过
    \u id
    重新构建您的
    产品列表
    数组

您需要在
$lookup
之前执行
$unwind
,因为它不会直接在数组字段中工作,这里您不需要$project中的$map

  • 产品列表
    解构数组
  • ,这将允许使用管道内部查找,此处
    $project
    到必填字段
  • $unwind
    使用路径
    产品列表。产品id
    ,因为您需要它作为对象
  • 通过
    \u id
    重新构建您的
    产品列表
    数组

查找后,输出结果是什么?输出结果与当前结果类似,只是显示了一些产品样本collection@varman,我已经更新了上面的产品集合。我正在检查,直到后来删除了我的答案。在查找之后,输出是什么?输出与当前结果类似。发布了一些产品样本collection@varman,我已经更新了上面的产品系列,我正在检查,直到删除我的答案你一分钟前收到了我:)Opps抱歉,伙计们,刚刚完成并发布。没有问题@turivishal:)继续摇摆all@turivishal哈哈,太棒了,兄弟,你现在做的一切都很好。。。。你不需要等任何人。很明显,我也在从答案中学习:DYou在一分钟前给了我:)哎呀,对不起,伙计们,刚刚完成并发布了。没有问题@turivishal:)继续摇摆all@turivishal哈哈,太棒了,兄弟,你现在做的一切都很好。。。。你不需要等任何人。显然,我也在从答案中学习:D