【发布时间】:2020-09-24 08:58:46
【问题描述】:
这是我使用 Mongo 的 JSON 数据:
{
"status": 1,
"message": "",
"data": [
{
"_id": "5f489968a26b303c54d0a174",
"name": "Mobile",
"SubCategory": [
{
"_id": "5f5f3827c8f0c718c01428d2",
"name": "55",
"Brand": [
{
"_id": "5f607898fea6362dc4eeaa5e",
"name": "sub category test2",
"Offer": []
},
{
"_id": "5f6078a4fea6362dc4eeaa5f",
"name": "brand2",
"Offer": [
{
"_id": "5f63839a1f7f3f2ec01cb19d",
"title": "qsw",
"Likes": [],
"Comments": [],
"Shares": []
}
]
},
{
"_id": "5f686deb08a1272b003aa3c7",
"name": "Marvel",
"Offer": [
{
"_id": "5f69ad691af8e2202447432c",
"title": "Marvel offer",
"Likes": [
{
"_id": "5f6af7a3bb67241d881c3d0b",
"updatedAt": "2020-09-03T07:18:14.073Z"
},
{
"_id": "5f6af7afbb67241d881c3d0c",
"updatedAt": "2020-09-03T07:18:14.073Z"
}
],
"Comments": [],
"Shares": []
},
{
"_id": "5f69b124d1c0f13b806ea20d",
"title": "Marvel offer2",
"Likes": [
{
"_id": "5f6af7c5bb67241d881c3d0d",
"updatedAt": "2020-09-03T07:18:14.073Z"
},
{
"_id": "5f6af7cabb67241d881c3d0e",
"updatedAt": "2020-09-03T07:18:14.073Z"
}
],
"Comments": [
{
"_id": "5f6af7d5bb67241d881c3d0f",
"updatedAt": "2020-09-03T06:31:36.930Z"
}
],
"Shares": []
}
]
}
]
}
]
},
{
"_id": "5f4899c2a26b303c54d0a175",
"name": "Computer",
"SubCategory": []
},
{
"_id": "5f489ba4ce0bd10c2c7af0eb",
"name": "Clothing",
"SubCategory": []
},
{
"_id": "5f48ab999579ac3690cd6897",
"name": "abc",
"SubCategory": []
},
{
"_id": "5f4c9c2b4630711ae099cec3",
"name": "abcd",
"SubCategory": []
},
{
"_id": "5f4cb13dc7fd2024ec020269",
"name": "Kitchen",
"SubCategory": []
},
{
"_id": "5f59e258a54fc924e02b0152",
"name": "q",
"SubCategory": []
},
{
"_id": "5f59e2bea54fc924e02b0153",
"name": "qq",
"SubCategory": []
},
{
"_id": "5f59e345a54fc924e02b0154",
"name": "w",
"SubCategory": []
},
{
"_id": "5f59e361a54fc924e02b0155",
"name": "s",
"SubCategory": []
},
{
"_id": "5f59e37ca54fc924e02b0156",
"name": "Amitesh Category1",
"SubCategory": []
},
{
"_id": "5f59e506848bf9451cf4f32f",
"name": "laptop6",
"SubCategory": []
},
{
"_id": "5f59e5fba5ea37295c6b5f8b",
"name": "Electronic2",
"SubCategory": []
},
{
"_id": "5f59fa26b4c9893a5ca5e96f",
"name": "Electronic3",
"SubCategory": []
},
{
"_id": "5f59fa33b4c9893a5ca5e970",
"name": "Electronic4",
"SubCategory": []
},
{
"_id": "5f59fa48b4c9893a5ca5e971",
"name": "laptop",
"SubCategory": []
},
{
"_id": "5f59faaeb4c9893a5ca5e973",
"name": "amitesh c1",
"SubCategory": [
{
"_id": "5f5f68e563d63905b093ebca",
"name": "sub cat 1",
"Brand": []
}
]
},
{
"_id": "5f59fceeb4c9893a5ca5e976",
"name": "11",
"SubCategory": []
},
{
"_id": "5f59ffa1b4c9893a5ca5e977",
"name": "ccw",
"SubCategory": []
},
{
"_id": "5f5b7e1bab9a593bd6950aed",
"name": "test 1",
"SubCategory": []
},
{
"_id": "5f5b870b3d722c41cc2e5f2f",
"name": "qq wqwq wq",
"SubCategory": [
{
"_id": "5f5f680b578fea0b640fb754",
"name": "sub cat 2",
"Brand": []
}
]
},
{
"_id": "5f5b88033d722c41cc2e5f30",
"name": "cvcv",
"SubCategory": [
{
"_id": "5f5f384ac8f0c718c01428d3",
"name": "sub cat 4",
"Brand": [
{
"_id": "5f6078bafea6362dc4eeaa60",
"name": "brand2",
"Offer": [
{
"_id": "5f687de408a1272b003aa3c8",
"title": "Testing expire1 depndent1",
"Likes": [],
"Comments": [],
"Shares": []
}
]
},
{
"_id": "5f6078bffea6362dc4eeaa61",
"name": "brand3",
"Offer": []
},
{
"_id": "5f6078c2fea6362dc4eeaa62",
"name": "brand4",
"Offer": []
},
{
"_id": "5f6086c8fea6362dc4eeaa63",
"name": "brand 5",
"Offer": []
},
{
"_id": "5f608712fea6362dc4eeaa64",
"name": "xyz 5",
"Offer": [
{
"_id": "5f644628129ad62d301b4ca7",
"title": "Testing expire11",
"Likes": [
{
"_id": "5f50963ad4e7b82584f29718",
"updatedAt": "2020-09-03T07:18:14.073Z"
},
{
"_id": "5f50993117bdf8161875e534",
"updatedAt": "2020-09-03T07:21:08.659Z"
},
{
"_id": "5f5099770c5b262edcbc34f0",
"updatedAt": "2020-09-03T07:33:16.483Z"
}
],
"Comments": [
{
"_id": "5f50913a7480733a1cfb90d3",
"updatedAt": "2020-09-03T06:46:18.010Z"
},
{
"_id": "5f646ff7e9df61290c73369f",
"updatedAt": "2020-09-03T06:46:18.010Z"
},
{
"_id": "5f646fffe9df61290c7336a0",
"updatedAt": "2020-09-03T06:46:18.010Z"
},
{
"_id": "5f647008e9df61290c7336a1",
"updatedAt": "2020-09-03T06:46:18.010Z"
},
{
"_id": "5f64700fe9df61290c7336a2",
"updatedAt": "2020-09-03T06:46:18.010Z"
},
{
"_id": "5f647103e9df61290c7336a6",
"updatedAt": "2020-09-03T06:46:18.010Z"
}
],
"Shares": []
}
]
},
{
"_id": "5f61ea593a89c23ff074cc2f",
"name": "aaaaaa",
"Offer": [
{
"_id": "5f6330505c9ff214f8ed1bf1",
"title": "12",
"Likes": [],
"Comments": [
{
"_id": "5f508c55e0caf427404addea",
"updatedAt": "2020-09-03T06:31:36.930Z"
},
{
"_id": "5f647015e9df61290c7336a3",
"updatedAt": "2020-09-03T06:31:36.930Z"
},
{
"_id": "5f647079e9df61290c7336a4",
"updatedAt": "2020-09-03T06:31:36.930Z"
},
{
"_id": "5f6470bfe9df61290c7336a5",
"updatedAt": "2020-09-03T06:31:36.930Z"
}
],
"Shares": []
}
]
},
{
"_id": "5f69f5564e033228a41ed3fa",
"name": "Amitesh Kumar",
"Offer": []
}
]
},
{
"_id": "5f5f5cf2578fea0b640fb753",
"name": "sub cat 3",
"Brand": [
{
"_id": "5f61f3d62565d12a0c101c59",
"name": "qq",
"Offer": []
}
]
}
]
},
{
"_id": "5f5f7f528147c330a46b5ae5",
"name": "aa",
"SubCategory": []
},
{
"_id": "5f5f7f778147c330a46b5ae6",
"name": "qwerer",
"SubCategory": []
},
{
"_id": "5f5f7f848147c330a46b5ae7",
"name": "sasas",
"SubCategory": []
},
{
"_id": "5f5f7fa98147c330a46b5ae8",
"name": "a",
"SubCategory": []
},
{
"_id": "5f6071e352a7f61230cd77ad",
"name": "qw",
"SubCategory": []
},
{
"_id": "5f60727f52a7f61230cd77ae",
"name": "qqqq qq",
"SubCategory": []
},
{
"_id": "5f60cd856eaac2316cd66ce7",
"name": "Amitesh cat",
"SubCategory": []
},
{
"_id": "5f61bd9daffdaa06543a1102",
"name": "aaa",
"SubCategory": []
},
{
"_id": "5f63588f379ffc2e50a5f90a",
"name": "aa ewew wew",
"SubCategory": []
}
]
}
查询此数据是:
categoryModel.aggregate(
[
{
$lookup:
{
'from': subcategoryModel.collection.name,
"let": { "categoryId": "$_id" },
"pipeline": [
{ "$match": { "$expr": { "$eq": ["$category", "$$categoryId"] } } },
{ '$project': { 'name': 1, '_id': 1 } },
{ '$match': req.body.subCategory ? { '_id': ObjectId(req.body.subCategory) } : {} },
{
"$lookup": {
"from": BrandModel.collection.name,
"let": { "subCategoryId": "$_id" },
"pipeline": [
{ "$match": { "$expr": { "$eq": ["$subCategory", "$$subCategoryId"] } } },
{ '$project': { 'name': 1, '_id': 1 } },
{ '$match': req.body.brand ? { '_id': ObjectId(req.body.brand) } : {} },
{
"$lookup": {
"from": offerModel.collection.name,
"let": { "brandId": "$_id" },
"pipeline": [
{
"$match": {
"$expr": {
"$eq": ["$brand", "$$brandId"],
}
}
},
{ '$project': { 'title': 1, '_id': 1 } },
{ '$match': req.body.offer ? { '_id': ObjectId(req.body.offer) } : {} },
{
"$lookup": {
"from": likeModel.collection.name,
"let": { "offerId": "$_id" },
"pipeline": [
{ "$match": { "$expr": { "$eq": ["$offer", "$$offerId"] } } },
{ '$project': { 'updatedAt': 1, '_id': 1 } },
{
'$match': req.body.from && req.body.to ? {
'updatedAt': {
"$gte": new Date(req.body.from),
"$lte": new Date(req.body.to)
}
} : req.body.from ? {
'updatedAt': {
"$gte": new Date(req.body.from),
}
} : req.body.to ? {
'updatedAt': {
"$lt": new Date(req.body.to)
}
} : {}
},
],
"as": "Likes"
}
},
{
"$lookup": {
"from": CommentModel.collection.name,
"let": { "offerId": "$_id" },
"pipeline": [
{ "$match": { "$expr": { "$eq": ["$offer", "$$offerId"] } } },
{ '$project': { 'updatedAt': 1, '_id': 1 } },
{
'$match': req.body.from && req.body.to ? {
'updatedAt': {
"$gte": new Date(req.body.from),
"$lte": new Date(req.body.to)
}
} : req.body.from ? {
'updatedAt': {
"$gte": new Date(req.body.from),
}
} : req.body.to ? {
'updatedAt': {
"$lt": new Date(req.body.to)
}
} : {}
},
],
"as": "Comments"
}
},
{
"$lookup": {
"from": ShareModel.collection.name,
"let": { "offerId": "$_id" },
"pipeline": [
{ "$match": { "$expr": { "$eq": ["$offer", "$$offerId"] } } },
{ '$project': { 'updatedAt': 1, '_id': 1 } },
{
'$match': req.body.from && req.body.to ? {
'updatedAt': {
"$gte": new Date(req.body.from),
"$lte": new Date(req.body.to)
}
} : req.body.from ? {
'updatedAt': {
"$gte": new Date(req.body.from),
}
} : req.body.to ? {
'updatedAt': {
"$lt": new Date(req.body.to)
}
} : {}
},
],
"as": "Shares"
}
}
],
"as": "Offer"
}
}
],
"as": "Brand"
}
}
],
'as': 'SubCategory',
},
},
{
$project:
{
_id: 1,
name: 1,
SubCategory: 1
}
},
// { $unwind: '$Brand' },
{ $match: match },
// { $sort: sort },
// { $limit: pagination.perPage },
//{ $skip: pagination.skip },
])
我需要的是,只有那些有优惠的记录,以及它的类别和子类别,我也需要分享评论等优惠,我在谷歌上搜索我尝试了很多我达到上述代码的东西,但我现在卡住了。我是 MongoDb 的新手,即使我尝试重构 mongoQuery 的结果,但在任何 Ide 中都卡住了
【问题讨论】:
标签: javascript node.js mongodb mongoose