相关内容:https://docs.mongodb.com/manual/reference/operator/aggregation-array/
@JsonFormat(pattern = “yyyy-MM-dd”) date类型返回值的格式
1,{ $arrayElemAt: [ , ] } 指定下标获取数组
Project获取数组中的最后一个
{
"$project": {
"_id": "$_id",
"inpatients": {
" $arrayElemAt": ["$inpatients",-1 ]
},
"id_no": "$id_no",
"name": "$name",
}
}
{
“project”:{
“_id”:”_id”,
“inpatients”:{
“
arrayElemAt":["
inpatients”,-1]
},
“id_no”:”
idno”,“name”:”
name”,
}
}
2,{ $reverseArray: } 数组倒序
db.users.aggregate([
{
$project:
{
name: 1,
reverseFavorites: { $reverseArray: "$favorites" }
}
}
])
判断:
The ifNull expression has the following syntax:
{ifNull: [ , ] }
db.inventory.aggregate(
[
{
$project: {
item: 1,
description: { $ifNull: [ "$description", "Unspecified" ] }
}
}
]
)
cond:判断<–布尔表达式–>返回两个指定的返回表达式之一。
{cond: { if: , then: , else: } }
Or:
{ $cond: [ , , ] }
db.inventory.aggregate(
[
{
$project:
{
item: 1,
discount:
{
$cond: { if: { $gte: [ "$qty", 250 ] }, then: 30, else: 20 }
}
}
}
]
)