简体   繁体   中英

Mongoose query for nested schema

I have the following schema:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ProjectSchema = require('./project.js')

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

const  ClientManager = mongoose.model('clientManager' , ClientManagerSchema);

module.exports = ClientManager; 

Inside the clientmanager schema, there is another as you can see. I want to query the database based on a value inside the ProjectSchema.

I am not sure how to do this but I've tried something like:

const find = () => {
   ClientManagers.find({ProjectSchema}).then(e => {
       console.log(e);
   });
}

however, this gives me an empty array.

Easy-peasy you can refer with dot notation:

const result = await ClientManager.find({ 'project.projectName': 'Foo' })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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