Mongodb if then condition under filter how to make
问题 db.main.aggregate([ { $lookup: { from: "history", localField: "history_id", foreignField: "history_id", as: "History" } }, { $project: { "History": { $filter: { input: "$History", as: "his", if: { $eq: [ "5e4a8d2d3952132a08ae5764", "$$his.user_id" ] }, then: { cond: { $and: [ { $lt: [ "$$his.date", "$date" ] }, { $eq: [ "5e4a8d2d3952132a08ae5764", "$$his.user_id" ] } ] } }, else: {} } }, data: 1, history_id: 1, sender_id: 1, text: 1, date: 1 } }, { $unwind: "$History" } ]) MongoPlayground