【问题标题】:Mongoose query for nested schema嵌套模式的猫鼬查询
【发布时间】:2020-08-04 21:24:16
【问题描述】:

我有以下架构:

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 中的值查询数据库。

我不知道该怎么做,但我尝试过类似的方法:

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

但是,这给了我一个空数组。

【问题讨论】:

    标签: javascript node.js mongodb mongoose


    【解决方案1】:

    简单易懂,您可以使用点符号来参考:

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

    【讨论】:

      猜你喜欢
      • 2021-03-23
      • 2019-03-22
      • 2019-04-05
      • 2019-07-20
      • 1970-01-01
      • 1970-01-01
      • 2014-06-22
      • 2018-02-05
      • 1970-01-01
      相关资源
      最近更新 更多