【问题标题】:mongoose populate not populating an array猫鼬填充不填充数组
【发布时间】:2015-07-13 12:42:57
【问题描述】:

我面临一个问题,即 mongoose 查询未填充数组类型。

这里是研究所架构

'use strict';

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var InstituteSchema = new Schema({
    name: String,
    address: String,
    city: String,
    country: String,
    zip: String,
    owner: { type: mongoose.Schema.ObjectId, ref: 'User' },
    teachers: [{type: mongoose.Schema.ObjectId, ref: 'Teacher'}],
    categories: [String],
    created : { type : Date, default : Date.now }
});

module.exports = mongoose.model('Institute', InstituteSchema);

这里是教师架构

'use strict';

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var TeacherSchema = new Schema({
    education: [{degree: String, instituteName: String}],
    dob: Date,
    photoUrl: String,
    phoneNumber: String,
    owner: {type: mongoose.Schema.ObjectId, ref: 'User'},
    institutes: [{type: mongoose.Schema.ObjectId, ref: 'Institute'}],
    subjects: [{type: mongoose.Schema.ObjectId, ref: 'Subject'}],
    created : { type : Date, default : Date.now }
})

module.exports = mongoose.model('Teacher', TeacherSchema);

这里是通过owner id查询机构的方法

exports.mine = function (req, res, next) {
    var ObjectId = mongoose.Types.ObjectId;
    var userId = new ObjectId(req.user._id);
    Institute.find({
        owner: userId
    }).populate('teachers').exec(function (err, institute) {
        if (err) return next(err);
        if (!institute) return res.json(401);
        res.json(institute);
    });
};

我可以从数据库中看到学院增加了老师

db.institutes.find();
{ 
    "_id" : ObjectId("554719a9f5be11c6d4369264"), 
    "owner" : ObjectId("5547199bf5be11c6d4369263"), 
    "country" : "USA", 
    "name" : "Raghvendra Singh", 
    "address" : "38589 Royal Ann Cmn", 
    "city" : "Fremont", 
    "zip" : "94536", 
    "created" : ISODate("2015-05-04T07:03:05.569Z"), 
    "categories" : [ "IIT", "Medical" ], 
    "teachers" : [ ObjectId("55471965f5be11c6d436925f") ], 
    "__v" : 3 
}

但不知何故,查询方法没有填充教师集合。奇怪的是,我什至没有得到带有对象 ID 的集合,它返回并使用空的教师数组进行设置。

当我从方法调用中删除 .populate('teachers') 时,它确实返回了带有对象 ID 的教师数组。

我查看了文档,但看不出我做错了什么。

【问题讨论】:

  • Teachers 文件至少需要加载一次,然后才能调用 mine 函数,以便向 Mongoose 注册模型。在调用该函数之前,您是否在某处加载了该模块?
  • 我有“var Teacher = require('../teacher/teacher.model.js');”在具有 mine 功能的文件的开头。 @BrianShamblen 这就是你的意思吗?
  • 是的...一切看起来都是正确的。我要检查的唯一另一件事是验证存储在 teachers 数组中的 ObjectId 是否实际引用了教师集合中的有效值。除此之外,我没有看到任何明显的东西。
  • db.teachers.find( { _id: ObjectId("55471965f5be11c6d436925f") } ); 返回什么?
  • 你又在Teachers下有Institutes...会不会是圈引用的问题??你检查过你的日志吗?

标签: node.js mongodb mongoose


【解决方案1】:

首先,您需要稍微更改您的模型,以供 teachers 字段提及。

teachers: [ { teacher: { type: Schema.ObjectId, ref: "Teacher" } } ]

exports.mine = function (req, res, next) {
    var ObjectId = mongoose.Types.ObjectId;
    var userId = new ObjectId(req.user._id);
    Institute.find({
        owner: userId
    }).populate('**teachers.teacher**').exec(function (err, institute) {
        if (err) return next(err);
        if (!institute) return res.json(401);
        res.json(institute);
    });
};

然后,将您的填充参数更改为 teachers.teacher 。它会工作

【讨论】:

    猜你喜欢
    • 2015-07-13
    • 2016-10-10
    • 2014-04-19
    • 1970-01-01
    • 2020-01-17
    • 2020-01-30
    • 2021-05-06
    • 2019-06-05
    相关资源
    最近更新 更多