首页 > 解决方案 > 用猫鼬在嵌套数组中查找值

问题描述

我有以下架构:

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))

这给了我一个空数组..

标签: javascriptnode.jsmongoose

解决方案


把这个改成find({'companyName': 'test bv'})这个find({'project.companyName': 'test bv'})


推荐阅读