嵌套模式的猫鼬查询 [英] Mongoose query for nested schema

查看:45
本文介绍了嵌套模式的猫鼬查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下架构:

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; 

在 clientmanager 架构中,您可以看到另一个架构.我想根据 ProjectSchema 中的值查询数据库.

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 你可以用点表示法参考:

Easy-peasy you can refer with dot notation:

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

这篇关于嵌套模式的猫鼬查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆