AllianceMember.aggregate([{
$match : { ally_id : alliance._id } ,
$lookup:
{
from: 'users',
localField: 'user_id',
foreignField: '_id',
as: 'users'
}
}]).then((members) => {});
所以基本上我想要实现的是 get members that are in alliance
然后我将它们聚合为 users
table 。这部分代码有效。但现在我需要过滤members that are in alliance
来自alliances
收藏。
AllianceMember
_id
user_id
ally_id
Alliance
_id
title
leader
我需要在联盟页面中显示具有 ally_id
的用户等于 alliance_id
。没有$match
功能我让所有成员都出现在每个联盟中。
我遇到的错误 $match: Arguments must be aggregate pipeline operators
最佳答案
您搞乱了 $lookup
和 $match
阶段。
而且您也没有将 ally_id
从 String
转换为 mongoose ObjectId
应该是这样的
db.aggregate([
{ "$match": { "ally_id": mongoose.Types.ObjectId(alliance._id) } },
{ "$lookup": {
"from": "users",
"localField": "user_id",
"foreignField": "_id",
"as": "users"
}}
])
关于node.js - 聚合 mongoose nodejs $match 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53457861/