如何使用$if$else条件mongodb转换数据



如何使用$if$else条件MongoDB 转换数据

mongoPlayground

当我们没有数据时,我想设置一个对象

record: [
{
"text": "No Found",
}
]

您的查询似乎正在尝试匹配嵌入的History.user_id数组包含特定值且date字段大于History.date字段的文档。如果for that, you can use$和`,则不需要$cond'/$

db.main.aggregate([
{$lookup: {
from: "history",
localField: "history_id",
foreignField: "_id",
as: "History"
}},
{$unwind: "$History"},
{"$match": {
$expr: {
$and: [
{$in: [
"5e4a8d2d3952132a08ae5763",
"$History.user_id"
]},
{$gt: [
"$date",
"$History.date"
]}
]
}
}
}
])

您可以遵循代码

db.project.aggregate([
{
$project:{
title:1,
"author.middle":{
$cond:{
if:{$eq:["","$author.middle"]},
then:"$$REMOVE",
else:"$author.middle"
}
}
}
}
])

最新更新