Kevin.a
Kevin.a

Reputation: 4306

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". 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..

Upvotes: 0

Views: 25

Answers (2)

Sven.hig
Sven.hig

Reputation: 4519

change this find({'companyName': 'test bv'})to this find({'project.companyName': 'test bv'})

Upvotes: 2

Denerator
Denerator

Reputation: 451

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

Upvotes: 1

Related Questions