node.js - 如何在 mongo db 聚合中合并结果?
问题描述
我在数据库中有 4 个集合。我需要在各种条件下加入这些收藏。为此,我正在使用聚合框架。我得到以下结果
[
{
"_id": "5e8469bd48b0853468db6836",
"Goods": {
"no": "6 - 20/21",
"grnNo": "17 - 20/21",
"id": "28a82510-6f26-11ea-86ba-8fc4c02180c6"
},
"ProgressArray": {
"LabTests_Status": "In-Progress",
"Inwards_Status": "In-Progress",
"Goods_Status": "In-Progress",
"Productions_Status": "Completed"
},
"Inwards": [
{
"grnNo": "17 - 20/21",
"id": "f358f0b0-70e7-11ea-9d6d-e31c8d952ef7"
}
],
"Production": {
"no": "48 - 20/21",
"grnNo": "17 - 20/21",
"id": "68b93e70-7267-11ea-9a01-c7602a734a9a"
},
"LabTest": [
{
"no": "68 - 20/21",
"grnNo": "17 - 20/21",
"id": "32740cf0-6f29-11ea-90b2-c18de169456e"
}
],
"currentLocation": "LabTest"
},
{
"_id": "5e8469bd48b0853468db6836",
"Goods": {
"no": "6 - 20/21",
"grnNo": "17 - 20/21",
"id": "28a82510-6f26-11ea-86ba-8fc4c02180c6"
},
"ProgressArray": {
"LabTests_Status": "In-Progress",
"Inwards_Status": "In-Progress",
"Goods_Status": "In-Progress",
"Productions_Status": "Completed"
},
"Inwards": [
{
"grnNo": "17 - 20/21",
"id": "f358f0b0-70e7-11ea-9d6d-e31c8d952ef7"
}
],
"Production": {
"no": "49 - 20/21",
"grnNo": "17 - 20/21",
"id": "68b93e70-7267-11ea-9a01-c7602a734a9a"
}
"LabTest": [
{
"no": "68 - 20/21",
"grnNo": "17 - 20/21",
"id": "32740cf0-6f29-11ea-90b2-c18de169456e"
}
],
"currentLocation": "LabTest"
}
]
我需要将生产值加入到单个数组结果中。这是我的预期输出
[
{
"_id": "5e8469bd48b0853468db6836",
"Goods": {
"no": "6 - 20/21",
"grnNo": "17 - 20/21",
"id": "28a82510-6f26-11ea-86ba-8fc4c02180c6"
},
"ProgressArray": {
"LabTests_Status": "In-Progress",
"Inwards_Status": "In-Progress",
"Goods_Status": "In-Progress",
"Productions_Status": "Completed"
},
"Inwards": [
{
"grnNo": "17 - 20/21",
"id": "f358f0b0-70e7-11ea-9d6d-e31c8d952ef7"
}
],
"Production": [{
"no": "48 - 20/21",
"grnNo": "17 - 20/21",
"id": "68b93e70-7267-11ea-9a01-c7602a734a9a"
},
{
"no": "49 - 20/21",
"grnNo": "17 - 20/21",
"id": "68b93e70-7267-11ea-9a01-c7602a734a9b"}],
"LabTest": [
{
"no": "68 - 20/21",
"grnNo": "17 - 20/21",
"id": "32740cf0-6f29-11ea-90b2-c18de169456e"
}
],
"currentLocation": "LabTest"
}]
这是我的代码
async function getGrnDetails(userParam) {
var userss = User.aggregate([{
$match: {
grnNo: userParam.grnNo
}
}, {
$lookup: {
from: "goods",
let: { grnNo: "$grnNo", defaultAccountId: "$defaultAccountId" },
pipeline: [{
$match: {
$expr: {
$and: [{
$eq: ["$defaultAccountId", '$$defaultAccountId'],
$eq: ["$grnNo", '$$grnNo']
}]
}
}
}],
as: "Goods",
},
},
{
$lookup: {
from: "inwards",
let: { grnNo: "$grnNo", defaultAccountId: "$defaultAccountId" },
pipeline: [
{
$match: {
$expr: {
$and: [{
$eq: ["$defaultAccountId", '$$defaultAccountId'],
$eq: ["$grnNo", '$$grnNo']
}]
}
}
}
],
as: "Inwards",
}
},
{
$lookup: {
from: "productions",
let: { grnNo: "$grnNo", defaultAccountId: "$defaultAccountId" },
pipeline: [{
$match: {
$expr: {
$and: [{
$eq: ["$defaultAccountId", '$$defaultAccountId'],
$eq: ["$grnNo", '$$grnNo']
}]
}
}
}, ],
as: "Productions",
}
},
{
$unwind: {
"path": "$Productions",
preserveNullAndEmptyArrays: true
}
}, {
$unwind: {
"path": "$status",
preserveNullAndEmptyArrays: true
}
}, {
$unwind: {
"path": "$Goods",
preserveNullAndEmptyArrays: true
}
},
{
$unwind: {
"path": "$Inwards",
preserveNullAndEmptyArrays: true
}
}, {
$project: {
ProgressArray: {
LabTests_Status: "$status",
Inwards_Status: "$Inwards.status",
Goods_Status: "$Goods.status",
Productions_Status: "$Productions.status",
},
Goods: { no: '$Goods.no', grnNo: '$Goods.grnNo', id: '$Goods.id' },
Inwards: [{ no: '$Inwards.no', grnNo: '$Inwards.grnNo', id: '$Inwards.id' }],
Production: { no: '$Productions.no', grnNo: '$Productions.grnNo', id: '$Productions.id' },
LabTest: [{
no: '$no',
grnNo: '$grnNo',
id: '$id'
}],
currentLocation: {
$cond: {
if: {
$eq: ["$status", "Completed"]
},
then: {
$cond: {
if: {
$eq: ["$Inwards.status", "Completed"]
},
then: {
$cond: {
if: {
$eq: ["$Goods.status", "Completed"]
},
then: {
$cond: {
if: {
$eq: ["$Productions.status", "Completed"]
},
then: "Finished",
else: "Productions"
}
},
else: "Goods"
}
},
else: "Inwards"
}
},
else: "LabTest"
}
},
}
},
{ $sort: { _id: 1 } },
{ $limit: 2 }
]);
return await userss
}
我只是一个初学者。我不知道该怎么做。请有人帮我解决这个问题
解决方案
在我投入生产之前,我只需要存储在一个特定的数组中并总共显示,所以我使用了 SET 方法来完成它。
var userss = User.aggregate([{
$match: {
grnNo: userParam.grnNo
}
}, {
$lookup: {
from: "goods",
let: { grnNo: "$grnNo", defaultAccountId: "$defaultAccountId" },
pipeline: [{
$match: {
$expr: {
$and: [{
$eq: ["$defaultAccountId", '$$defaultAccountId'],
$eq: ["$grnNo", '$$grnNo']
}]
}
}
}],
as: "Goods",
},
},
{
$lookup: {
from: "inwards",
let: { grnNo: "$grnNo", defaultAccountId: "$defaultAccountId" },
pipeline: [
{
$match: {
$expr: {
$and: [{
$eq: ["$defaultAccountId", '$$defaultAccountId'],
$eq: ["$grnNo", '$$grnNo']
}]
}
}
}
],
as: "Inwards",
}
},
{
$lookup: {
from: "productions",
let: { grnNo: "$grnNo", defaultAccountId: "$defaultAccountId" },
pipeline: [{
$match: {
$expr: {
$and: [{
$eq: ["$defaultAccountId", '$$defaultAccountId'],
$eq: ["$grnNo", '$$grnNo']
}]
}
}
}, ],
as: "Productions",
}
},
{
$lookup: {
from: "despatches",
let: { grnNo: "$grnNo", defaultAccountId: "$defaultAccountId" },
pipeline: [{
$match: {
$expr: {
$and: [{
$eq: ["$defaultAccountId", '$$defaultAccountId'],
$eq: ["$grnNo", '$$grnNo']
}]
}
}
}, ],
as: "Despatch",
}
},
{
$lookup: {
from: "deliveries",
let: { grnNo: "$grnNo", defaultAccountId: "$defaultAccountId" },
pipeline: [{
$match: {
$expr: {
$and: [{
$eq: ["$defaultAccountId", '$$defaultAccountId'],
$eq: ["$grnNo", '$$grnNo']
}]
}
}
}, ],
as: "Delivery",
}
},
{
$unwind: {
"path": "$Productions",
preserveNullAndEmptyArrays: true
}
}, {
$unwind: {
"path": "$status",
preserveNullAndEmptyArrays: true
}
}, {
$unwind: {
"path": "$Goods",
preserveNullAndEmptyArrays: true
}
},
{
$unwind: {
"path": "$Inwards",
preserveNullAndEmptyArrays: true
}
},
{
$unwind: {
"path": "$Delivery",
preserveNullAndEmptyArrays: true
}
},
{
$unwind: {
"path": "$Despatch",
preserveNullAndEmptyArrays: true
}
},
{
$group: {
_id: null,
Inwards_Details: { $addToSet: { no: '$Inwards.no', grnNo: '$Inwards.grnNo', id: '$Inwards.id' } },
LabTest_Details: { $addToSet: { no: '$no', grnNo: '$grnNo', id: '$id' } },
Goods_Scheduler_Details: { $addToSet: { no: '$Goods.no', grnNo: '$Goods.grnNo', id: '$Goods.id' } },
Production_Details: { $addToSet: { no: '$Productions.no', grnNo: '$Productions.grnNo', id: '$Productions.id' } },
Despatch_Details: { $addToSet: { no: '$Despatch.no', grnNo: '$Despatch.grnNo', id: '$Despatch.id' } },
Delivery_Details: { $addToSet: { no: '$Delivery.no', grnNo: '$Delivery.grnNo', id: '$Delivery.id' } },
ProgressArray: {
$addToSet: {
Inwards_Status: "$Inwards_Details.status",
LabTest_Status: "$status",
Goodscheduler_Status: "$Goods.status",
Productions_Status: "$Productions.status",
Delivery_Status: "$Delivery.status",
Despatch_Status: "$Despatch.status"
}
},
},
},
{ $sort: { _id: 1 } },
{
$set: {
Inwards_Statuss: { $indexOfArray: ["$ProgressArray.Inwards_Status", "In-Progress"] }
}
},
{
$set: {
LabTest_Statuss: { $indexOfArray: ["$ProgressArray.LabTest_Status", "In-Progress"] }
}
},
{
$set: {
Goodscheduler_Statuss: { $indexOfArray: ["$ProgressArray.Goodscheduler_Status", "In-Progress"] }
}
},
{
$set: {
Productions_Statuss: { $indexOfArray: ["$ProgressArray.Productions_Status", "In-Progress"] }
}
},
{
$set: {
Deliveriess: { $indexOfArray: ["$ProgressArray.Delivery_Status", "In-Progress"] }
}
},
{
$set: {
Despatch_Statuss: { $indexOfArray: ["$ProgressArray.Despatch_Status", "In-Progress"] }
}
},
{
$set: {
ProgressArrays: {
Inwards_Status: {
$cond: {
if: {
$eq: ["$Inwards_Statuss", -1]
},
then: "Completed",
else: "In-Progress"
}
},
LabTest_Status: {
$cond: {
if: {
$eq: ["$LabTest_Statuss", -1]
},
then: "Completed",
else: "In-Progress"
}
},
Goods_Scheduler_Status: {
$cond: {
if: {
$eq: ["$Goodscheduler_Statuss", -1]
},
then: "Completed",
else: "In-Progress"
}
},
Productions_Status: {
$cond: {
if: {
$eq: ["$Productions_Statuss", -1]
},
then: "Completed",
else: "In-Progress"
}
},
Deliveries_Status: {
$cond: {
if: {
$eq: ["$Deliveriess", -1]
},
then: "Completed",
else: "Yet to be Started"
}
},
Despatch_Status: {
$cond: {
if: {
$eq: ["$Despatch_Statuss", -1]
},
then: "Completed",
else: "Yet to be Started"
}
},
}
}
},
{
$project: {
currentLocation: {
$cond: {
if: {
$eq: ["$ProgressArrays.Inwards_Status", "Completed"]
},
then: {
$cond: {
if: {
$eq: ["$ProgressArrays.LabTest_Status", "Completed"]
},
then: {
$cond: {
if: {
$eq: ["$ProgressArrays. Goods_Scheduler_Status", "Completed"]
},
then: {
$cond: {
if: {
$eq: ["$ProgressArrays.Productions_Status", "Completed"]
},
then: {
$cond: {
if: {
$eq: ["$ProgressArrays.Despatch_Status", "Completed"]
},
then: {
$cond: {
if: {
$eq: ["$ProgressArrays.Deliveries_Status", "Completed"]
},
then: "Finished",
else: "Delivery"
}
},
else: "Despatch"
}
},
else: "Production"
}
},
else: "Goods_Scheduler"
},
},
else: "LabTest"
},
},
else: "Inwards"
}
},
ProgressArray: "$ProgressArrays",
Inwards_Details: {
$cond: {
if: {
$eq: ["$Inwards_Details", [{}]]
},
then: [],
else: "$Inwards_Details"
}
},
LabTest_Details: {
$cond: {
if: {
$eq: ["$LabTest_Details", [{}]]
},
then: [],
else: "$LabTest_Details"
}
},
Goods_Scheduler_Details: {
$cond: {
if: {
$eq: ["$Goods_Scheduler_Details", [{}]]
},
then: [],
else: "$Goods_Scheduler_Details"
}
},
Production_Details: {
$cond: {
if: {
$eq: ["$Production_Details", [{}]]
},
then: [],
else: "$Production_Details"
}
},
Despatch_Details: {
$cond: {
if: {
$eq: ["$Despatch_Details", [{}]]
},
then: [],
else: "$Despatch_Details"
}
},
Delivery_Details: {
$cond: {
if: {
$eq: ["$Delivery_Details", [{}]]
},
then: [],
else: "$Delivery_Details"
}
}
}
}
]).allowDiskUse(true);;
推荐阅读
- android - 在 anr traces.txt 中,'held mutexes=' 是什么意思?
- javascript - ReactJs 切换案例
- java - 尝试使用 Spring 上传多个 Multipart 文件
- windows - 如何自动设置最小和最大分页文件大小?
- ios - 斯威夫特 5 | UINavigationController 直到进入 DetailsViewController 才出现
- r - R data.table fread select与部分字符串匹配?
- python - Python:求数字的平均值
- javascript - 为什么这个函数在 React Native 中返回 false?
- html - Bulma 中的列是水平放置的,我们可以固定一行中的列数并在另一行中休息吗?
- swift - 你如何测试一个 observable 在失败时是否重试 X 次?