node.js - 聚合猫鼬 nodejs $match 不起作用
问题描述
AllianceMember.aggregate([{
$match : { ally_id : alliance._id } ,
$lookup:
{
from: 'users',
localField: 'user_id',
foreignField: '_id',
as: 'users'
}
}]).then((members) => {});
所以基本上我想要实现的是得到然后我将它们与表格members that are in alliance
聚合起来。users
这部分代码有效。但现在我需要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
fromString
转换成 mongooseObjectId
它应该是这样的
db.aggregate([
{ "$match": { "ally_id": mongoose.Types.ObjectId(alliance._id) } },
{ "$lookup": {
"from": "users",
"localField": "user_id",
"foreignField": "_id",
"as": "users"
}}
])
推荐阅读
- amazon-web-services - 使用 JavaScript 从 AWS S3 存储桶读取数据
- access-token - 具有自定义生命周期的身份服务器 4 访问令牌
- android - 处理命令时发生未知的服务器端错误原始错误:找不到 adb.exe in
- python-3.6 - 无法在 Python 3.6 中运行 dlib 模块
- javascript - dot.js 使用数组
- java - 测试包中的类在 IntelliJ 编译时不可用
- javascript - 在div中加载外部php文件时如何控制两个滚动条出现在单个屏幕上
- html - 表格/两个半块中的表单字段,使用“选项卡”导航
- java - 我在 JADE 主容器 GUI 的 CLASSPATH 中找不到我的代理类
- c++ - 用字母实现 C++ 中的 Prim 算法