我有两个表:游戏和问题。 游戏对象如下:
{
_id: ObjectId(...),
items: [
{_id: ObjectId(...), title: 'foo'}
],
}
问题:
{_id: ObjectId(...), variants: [...], ...}
我的查询:
let query = {};
...
query = [
{ $match: query },
];
if (gameId) {
query = concat(query, [
{
$lookup: {
from: 'games',
let: { gameId: filter.gameId },
pipeline: [
{ $match: { _id: ObjectId(filter.gameId) }},
],
as: 'games',
},
},
{
$addFields: {
answersCnt: {
$size: { $ifNull: [ '$variants', [] ] },
},
choosen: { $in: ['$_id', '$games.items'] },
},
},
]);
}
问题是游戏。项目不是ID数组,而是对象数组。 字段answersCnt已添加,但我对选择的查询有疑问。如果在game.items中此项目_id,我需要设置true或false。项目是对象数组。感谢您的帮助!
最后,查询数组将聚集在Question.aggregate(query)
中