Mic*_*ard 6 mongoose mongodb node.js angularjs mongoose-populate
好的.所以我试图做一些嵌套的mongoose模型.我发现这Mongoose-Deep-Populate是嵌套解决方案最简单的修复方法之一,但是,尽管看起来很简单,但我无法弄清楚如何正确注册插件.
我正在使用一个angular-fullstack项目,只是添加npm install了deep-populate插件.然后我进入我的模型并添加了这个:
'use strict';
var deepPopulate = require('mongoose-deep-populate');
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var EntrySchema = new Schema({
articleId: Number,
title: String,
content: String,
date: Date,
children: [{
type: Schema.ObjectId,
ref: 'Entry'
}],
forum: {
type: Schema.ObjectId,
ref: 'Forum'
},
language: {
type: Schema.ObjectId,
ref: 'Language'
},
orphan: Boolean,
writer: {
type: Schema.ObjectId,
ref: 'User'
},
type: String
}).plugin(deepPopulate, options);
module.exports = mongoose.model('Entry', EntrySchema);
Run Code Online (Sandbox Code Playgroud)
如您所见entries,我的模型中可能有多层嵌套.为了找到它们,我写了这个:
// Get list of chapter entries
exports.chapter = function(req, res) {
Entry.find()
.where('orphan').equals(true)
.populate('children')
.exec(function (err, entrys) {
Entry.deepPopulate(docs, 'children', err, entrys) {
if(err) {
return handleError(res, err);
}
return res.json(200, entrys);
};
});
};
Run Code Online (Sandbox Code Playgroud)
但这不起作用.而且我很确定我做错了什么.我已经检查了网络,但似乎无法找到一个可以澄清我的错误的例子.而且我现在来找你伟大的主人来解决我的麻烦.
小智 0
试试这个代码
exports.chapter = function(req, res) {
Entry.find()
.where('orphan').equals(true)
.populate('children')
.exec(function (err, entrys) {
mongoose.deepPopulate(entrys, 'children', err, function(err, entrys) {
if(err) {
return handleError(res, err);
}
return res.json(200, entrys);
});
});
};Run Code Online (Sandbox Code Playgroud)
如果你遇到任何困难。请告诉我
| 归档时间: |
|
| 查看次数: |
642 次 |
| 最近记录: |