提问者:小点点

用mongoose在嵌套数组中查找值


我有以下架构:

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

这给了我一个空数组。


共2个答案

匿名用户

ClientManager.find({'project.companyName': 'test bv'}).then((res) => console.log(res)).catch(err => console.log(err))

匿名用户

将此查找({'company name':'test bv'})更改为此查找({'project.companyname':'test bv'})