简体   繁体   English

使用 mongoose 在嵌套数组中查找值

[英]find value in nested array with mongoose

I have the following schema:我有以下架构:

const ClientManagerSchema = new Schema({
    name : { type : String,  required : true},
    project : [ProjectSchema]
});

The project one looks like this:项目一看起来像这样:

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 : [],
});

I want to select all documents from my database that have the company name "test bv".我想 select 我的数据库中所有具有公司名称“test bv”的文档。 But since the projects value is nested im not sure how to do it.但由于项目价值是嵌套的,我不知道该怎么做。 I could also lift the value to a level where i can easily access it but thats not optimal.我还可以将值提升到我可以轻松访问它的水平,但这不是最佳的。

I tried some things which didn't work:我尝试了一些不起作用的东西:

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

This gave me an empty array..这给了我一个空数组..

change this find({'companyName': 'test bv'}) to this find({'project.companyName': 'test bv'})将此find({'companyName': 'test bv'})更改为此find({'project.companyName': 'test bv'})

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM