我正在使用 MongoDB shell 版本 v3.6.3。我有两个集合 1. 用户 2. 业务 我在下面给出了示例数据。
user
********
_id : 1
username : "joyjeba2"
mobile_number : 9840347197,
profile_url :"http://localhost:3001/user/1599214209351_dp1.jpg"
saved_products :[1,2]
Business
*****
_id:1
name : "businessname1"
location :"location",
contact_number:123456,
Products : [
{ "name": "product",
"tags": [
"shoes",
"slippers"
],
"description": "its a simple description",
"lower_price": 20,
"higher_price": 30,
"min_order": 20,
"units": "count",
"media_urls": [
"http://localhost:3001/product/1586703106075_DP1.jpg"
],
"_id": 1
}
{ "name": "product",
"tags": [
"shoes",
"slippers"
],
"description": "its a simple description",
"lower_price": 20,
"higher_price": 30,
"min_order": 20,
"units": "count",
"media_urls": [
"http://localhost:3001/product/1586703106075_DP1.jpg"
],
"_id": 2
},
]
现在我想加入从saved_products
用户集合到业务集合中的产品。
预期结果是:
_id : 1
username : "joyjeba2"
mobile_number : 9840347197,
profile_url :"http://localhost:3001/user/1599214209351_dp1.jpg"
saved_product : [
{ "name": "product",
"tags": [
"shoes",
"slippers"
],
"description": "its a simple description",
"lower_price": 20,
"higher_price": 30,
"min_order": 20,
"units": "count",
"media_urls": [
"http://localhost:3001/product/1586703106075_DP1.jpg"
],
"_id": 1
"_business_id":1,
"business_name" : "businessname1"
"location" :"location"
}
{ "name": "product",
"tags": [
"shoes",
"slippers"
],
"description": "its a simple description",
"lower_price": 20,
"higher_price": 30,
"min_order": 20,
"units": "count",
"media_urls": [
"http://localhost:3001/product/1586703106075_DP1.jpg"
],
"_id": 2,
"_business_id":1,
"business_name" : "businessname1"
"location" :"location"
},
],
当产品是单独的集合时(在查找和展开的帮助下),我能够做到这一点。但是这里的产品作为嵌套文档位于业务集合中。我怎样才能做到这一点。请帮帮我。