node.js - 如果现有数组中列出了另一个属性,如何在 Mongoose 中投影一个新的布尔字段?
问题描述
考虑 Mongoose 中的查询:
let StudentCodes = .... // getting this from somewhere
await Students.aggregate(
[
{
$project: {
StudentCODE: "$StudentCODE",
StudName: "$StudName",
StudProfileDesc: "$StudProfileDesc",
IsReviewed: {
$cond: [{ $eq: [StudentCodes, "$StudentCODE"] }, 1, 0]
}
}
}
],
function(err, results) {
if (err) {
console.log(err);
}
console.log(results);
return res.status(200).json(results);
}
);
如果属性存在于数组中,IsReviewed
我们如何进行投影true
?false
StudentCODE
StudentCodes
解决方案
尝试如下,您可以使用$in in$cond
来做到这一点:
let StudentCodes = .... // getting this from somewhere
await Students.aggregate(
[
{
$project: {
StudentCODE: "$StudentCODE",
StudName: "$StudName",
StudProfileDesc: "$StudProfileDesc",
IsReviewed: {
$cond: [{ $in: ["$StudentCODE", StudentCodes] }, true, false]
}
}
}
],
function (err, results) {
if (err) {
console.log(err);
}
console.log(results);
return res.status(200).json(results);
}
);
推荐阅读
- python-3.x - 如何修复 Plotly Dash 中的“下拉菜单读取”错误
- php - 从给定 2 个数组中选择匹配的 id 数组
- php - PHP - Laravel 返回字符串而不是数组(JSON)
- php - 主要内容未在 codeigniter 中显示
- php - Laravel 5.0 显示数组中键的值与值匹配的项目
- apache - 在 APACHE 中不使用协议进行重定向
- c++ - 另一个命名空间中函数的 C++ ADL
- html - 如何防止我的 td 堆叠在彼此下方?
- python - 在 Python 中剥离具有特定范围的 HTML 文件
- c# - 使用SignalR时如何访问hangfire中的Session(IDashboardAuthorizationFilter的实现中的Authrize方法)?