所以我有一些像Survey Schema(我使用的是猫鼬)。
在这个Schema中,对于每个投票选项,我有一个包含ObjectIds of Users的votes []数组。
现在我想检查用户是否可以投票…
您可以使用 $ addFields 和 $地图 覆盖现有的 options 领域。检查是否 userId 存在于 votes 您可以使用的数组 $ indexOfArray
options
userId
votes
SurveySchema.aggregate([ { $addFields: { options: { $map: { input: "$options", in: { value: "$$this.value", votes: "$$this.votes", didIVote: { $ne: [ { $indexOfArray: [ "$$this.votes", userId ] }, -1 ] } } } } } } ])