node.js - mongodb 未知顶级运算符:$function nodejs
问题描述
我试图仅提取用户未选择的那些集合。这是我的代码。
return await Campaign.aggregate([
{
$lookup: {
from:"influncer_campaign_mapping",
localField: "campaignId",
foreignField:"campaignId",
as: "icmapping"
}
},
{$unwind:"$icmapping"},
{
$match: {$function:{
body: function(icmapping) {
if(icmapping != null || icmapping != undefined){
return {"icmapping.userId":{$ne:"ccdc7d1a-9cdb-4768-b8c1-0446e35576fb"}}
}else{ return null}
},
args: [ "$icmapping" ],
lang: "js"
}
}
}
])
我的映射表
{
influncerCampaignMappingId: { type: String, require: true },
userId: { type: String, require: true },
campaignId: { type: String, require: true },
// isSelected: { type: Boolean, require: true, default: true },
isDeleted: { type: Boolean, require: true },
deletedDate: { type: Date, require: true },
},
这是活动表,我在其中存储活动列表。
{
campaignId: { type: String, require: true },
name: { type: String, require: true },
image: { type: String, require: true },
serviceName: { type: String, require: true },
description: { type: String, require: true },
categoryId: [{ type: String }],
keywords: [{ type: String }]
}
实际上,我正在考虑来自活动表的活动 ID 和来自用户表的用户 ID,并将其插入映射表中。现在的目标是仅实现用户之前未选择的那些集合(列表),以便他们只能看到他们尚未选择的那些活动(列表)。
解决方案
试试这样,希望有帮助
return await Campaign.aggregate([{
$lookup: {
from: "influncer_campaign_mapping",
localField: "campaignId",
foreignField: "campaignId",
as: "icmapping"
}
},
{
$unwind: "$icmapping"
},
{
$match: {
$and:
[{
$ne: ["icmapping.userId", "ccdc7d1a-9cdb-4768-b8c1-0446e35576fb"],
$ne: ["icmapping.userId", null]
}]
}
}
])
推荐阅读
- php - 未定义变量:Blade Laravel 7.x 中的横幅
- java - 需要特定时间格式的 XMLGregorianCalendar| 爪哇
- python - 将文本格式的 PNG 转换回文件对象
- c - 程序会遇到未定义的行为吗?
- python - 如何使发布请求与 ngrok 一起使用?
- php - 我如何在 Laravel 中区分两个字符串并将其传递给视图?
- ios - 在ios中截屏时隐藏视图
- python - 如果我已经安装了 anaconda,如何从命令行通过 jupyter notebook 访问 python 文件?
- javascript - jQuery 问题。如何敲击键盘以使其正常工作
- mongodb - 由于“无法连接到新的副本集主服务器”错误,无法从 mongo shell 连接集群