簡體   English   中英

使用 mongoose 在嵌套數組中查找值

[英]find value in nested array with 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 : [],
});

我想 select 我的數據庫中所有具有公司名稱“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'})

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