嵌套模式的猫鼬查询
问题描述:
我有以下架构:
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' })