javascript - 用猫鼬在嵌套数组中查找值
问题描述
我有以下架构:
const ClientManagerSchema = new Schema({
name : { type : String, required : true},
project : [ProjectSchema]
});
项目一看起来像这样:
const ProjectSchema = new Schema({
companyName : {type: String , required : true},
projectName : String,
projectManager : String,
projectManagerUrl : String,
employees : [],
contactPerson : [],
employeeInfo : [],
projectHours : [],
trelloUrl : String,
dataStudioUrl : String,
projectUrl : String,
AnalyticsEmail : String,
companyId : String,
projectId : String,
total : Number,
totalIn : Number,
totalSt : Number,
totalSale : Number,
earliestDate : String,
firstEvaluation : String,
secondEvaluation : String,
firstEvaluationIndex : Number,
secondEvaluationIndex : Number,
revenueGroups : [RevenueGroupSchema],
revenueGroupsIn : [RevenueGroupSchema],
revenueGroupsSt : [RevenueGroupSchema],
sales : [RevenueGroupSchema],
saleData : [],
});
我想从我的数据库中选择公司名称为“test bv”的所有文档。但由于项目价值是嵌套的,我不知道该怎么做。我还可以将值提升到我可以轻松访问它的水平,但这不是最佳的。
我尝试了一些不起作用的东西:
ClientManager.find({'companyName': 'test bv'}).then((res) => console.log(res)).catch(err => console.log(err))
这给了我一个空数组..
解决方案
把这个改成find({'companyName': 'test bv'})
这个find({'project.companyName': 'test bv'})
推荐阅读
- javascript - 创建具有多个基本 url 的 Javascript API 类
- node.js - 如何知道是否存在一个名为“A”的顶点,其中包含数据?
- mongodb - 如何制作 sum(field)-sum(field2) 并获得所有文档的单个结果
- javascript - 如何检查是否在php中检查了span od xheckbox?
- c - 清除分配给C中字符串的内存?
- tfs - Azure devops 管道初始化作业失败并显示错误消息“#[error]bad content type”
- c# - Treeview - 在放置在 NodeMouseClick 事件上的消息弹出窗口上单击取消时无法恢复先前的选择
- php - 上传带有亚洲字符的 CSV 文件,特别是“泰语”返回 ??? 在上传的文件上
- c# - 如何在我的列表中存储每个单独的正则表达式匹配主题标签?
- c# - 纸、剪刀、石头如何显示正确的结果?