简体   繁体   English

Mongoose 查询嵌套模式

[英]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.在 clientmanager 模式中,还有另一个如您所见。 I want to query the database based on a value inside the ProjectSchema.我想根据 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' })

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

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