Skip to content

Commit

Permalink
update-demo
Browse files Browse the repository at this point in the history
  • Loading branch information
yznote committed Sep 5, 2024
1 parent 07f72c5 commit ee4ad4a
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions _posts/2024-09-05-数据库聚合操作.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ async list() {
normoney: {
$sum: {
$cond: {
if: {
$eq: ["$inctype", 0]
},
if: { $eq: ["$inctype", 0] },
then: "$money",
else: 0
}
Expand All @@ -187,9 +185,7 @@ async list() {
extmoney: {
$sum: {
$cond: {
if: {
$eq: ["$inctype", 1]
},
if: { $eq: ["$inctype", 1] },
then: "$money",
else: 0
}
Expand All @@ -198,25 +194,22 @@ async list() {
allmark: {
$addToSet: {
$cond: {
if: {
$ne: ["$mark", ""]
},
then: {
$concat: ["$mark", ";"]
},
if: { $ne: ["$mark", ""] },
then: { $concat: ["$mark", ";"] },
else: ""
}
}
}
})
.addFields({
// 同示例1区别
normoney: { $divide: ["$normoney", 100] },
extmoney: { $divide: ["$extmoney", 100] },
allmark: {
$reduce: {
input: "$allmark",
initialValue: "",
in: {
$concat: ["$$value", "$$this"]
}
in: { $concat: ["$$value", "$$this"] }
}
}
})
Expand All @@ -230,15 +223,9 @@ async list() {
.addFields({
works: {
$cond: {
if: {
$eq: [{
$size: "$works"
}, 0]
},
if: { $eq:[ {$size: "$works"}, 0] },
then: {},
else: {
$arrayElemAt: ["$works", 0]
}
else: { $arrayElemAt: ["$works", 0] }
}
}
})
Expand All @@ -248,9 +235,14 @@ async list() {
// 同示例1区别
.group({
_id:"$_id.year",
normoney: { $sum: "$normoney" },
extmoney: { $sum: "$extmoney" },
data:{$push:"$$ROOT"}
})
// 同示例1区别
.addFields({
total:{ $sum: ["$normoney", "$extmoney"] }
})
.sort({
_id: -1
})
Expand All @@ -261,7 +253,8 @@ async list() {
return {
errSubject: '',
errCode: 0,
errMsg: 'ok'
errMsg: 'ok',
data:res.data,
}
}
```
Expand All @@ -273,13 +266,15 @@ async list() {
"data": [
{
"_id": "2024",
"normoney": 10500,
"extmoney": 1200,
"data": [
{
"_id": {
"year": "2024",
"date": "2024-09"
},
"normoney": 500000,
"normoney": 5000,
"extmoney": 0,
"allmark": "",
"works": {
Expand All @@ -298,14 +293,15 @@ async list() {
"year": "2024",
"date": "2024-08"
},
"normoney": 550000,
"extmoney": 120000,
"normoney": 5500,
"extmoney": 1200,
"allmark": "111;",
"works": {

}
}
]
],
"total": 11700
}
]
}
Expand Down

0 comments on commit ee4ad4a

Please sign in to comment.