0
進行查找是否有可能在MongoDB中進行查找,但只在一定的條件?我需要的事件構成了我的MongoDB,只有當事件被某些用戶添加相關的賭注 - 我想賭添加到收藏。如何在一定的條件
首先,我用匹配操作符和$查找,以獲得有關事件的賭注,但問題是我需要定義第二個條件 - 賭注的用戶標識必須是相同的外部參數(用戶ID)的值。
然後我需要添加只有一個結果,不是一個集合 - 這就是爲什麼我用$項目。請幫忙。
我的代碼放在這裏:
db.collection('events').aggregate([
{
$match: {
'isDeleted': false,
'leagueId': ObjectID(leagueId)
}
},
{
$lookup: {
from: "bets",
localField: "_id",
foreignField: "eventId",
as: "bets"
},
},
{
$project: {
_id: true,
home: true,
guest: true,
description: true,
result: true,
resultHome: true,
resultGuest: true,
startDate: true,
type: true,
specialPoints: true,
leagueStage: true,
createdDate: true,
updatedDate: true,
isDeleted: true,
isCalculated: true,
leagueId: true,
bet: { "$arrayElemAt": [ "$bets", 0 ] }
}
}