我正在努力寻找关于猫鼬儿童人口的例子或文件。
http://mongoosejs.com/docs/populate.html
关于他们的文件:
var mongoose = require('mongoose')
, Schema = mongoose.Schema
var personSchema = Schema({
_id : Number,
name : String,
age : Number,
stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});
var storySchema = Schema({
_creator : { type: Number, ref: 'Person' },
title : String,
fans : [{ type: Number, ref: 'Person' }]
});
var Story = mongoose.model('Story', storySchema);
var Person = mongoose.model('Person', personSchema);
这是有道理的,一个人可以有很多故事,因此有“父母”字段“故事”,其中列有所有故事。
我正在努力理解的是你如何将故事推向个人架构。
例如,我有分配方案:
var mongoose = require ( 'mongoose' ),
Schema = mongoose.Schema;
var assignmentSchema = new Schema (
{
_id: String,
assignName: String,
modInsID: [{ type: Schema.Types.Number, ref: 'ModuleInst' }],
studentAssigns: [{type: Schema.Types.ObjectId, ref: 'StudentAssign' }]
}
);
module.exports = mongoose.model ( 'Assignment', assignmentSchema );
studentAssigns
存储studentAssigns
的所有ID,然后可以与.pre
中间件一起使用以进行级联删除。
现在我的StudentAssign架构:
var mongoose = require ( 'mongoose' ),
autoIncrement = require ( 'mongoose-auto-increment' ),
Schema = mongoose.Schema;
var connection = mongoose.createConnection("************");
autoIncrement.initialize(connection);
var studentAssignSchema = new Schema (
{
assID: [{ type: Schema.Types.String, ref: 'Assignment' }],
studentID: [{ type: Schema.Types.Number, ref: 'Student' }]
}
);
var StudentAssign = connection.model('StudentAssign', studentAssignSchema);
module.exports = mongoose.model ('StudentAssign', studentAssignSchema );
正如您所看到的那样,它已经引用了“Assignment
”
这是我的api代码:
studentAssign POST:
router.route('/student-assignment').post( function(req, res) {
var studentAssign = new StudentAssign();
studentAssign.assID = req.body.assID;
studentAssign.studentID = req.body.studentID;
studentAssign.save(function(err, studentAssign) {
if(err) console.log(err);
res.status(200).json(studentAssign);
});
})
这就是我感到困惑的部分,我将“studentAssign
”推入“Assignment
”架构的'studentAssigns
数组?
这是我目前的api json回调:
[
{
"_id": "As 1",
"assignName": "Software Implementation",
"__v": 0,
"studentAssigns": [],
"modInsID": [
{
"_id": 22,
"semester": "TRI 3",
"year": 2016,
"__v": 0,
"modID": [
111
]
}
]
}
]
文档只是没有说清楚,因为它们只显示:
aaron.stories.push(story1);
aaron.save(callback);
没有解释?
我试过:
var assignment = new Assignment();
assignment.studentAssigns.push(studentAssign);
并没有存储任何内容??
答案 0 :(得分:0)
您建模状态:
assID: [{ type: Schema.Types.String, ref: 'Assignment' }],
studentID: [{ type: Schema.Types.Number, ref: 'Student' }]
我认为从您的代码中您不想在assID中存储多个作业,而在studentID中存储多个学生。将模型修改为
assID: { type: Schema.Types.String, ref: 'Assignment' },
studentID: { type: Schema.Types.Number, ref: 'Student' }
您的保存代码可以保持不变。
如果您确实要存储多个分配,则需要将它们推入assID数组; - )
答案 1 :(得分:0)
router.get('/author', (req, res, next) => {
Person.
find().
exec( (err, person) => {
if (err) return handleError(err)
Story.find({author: person[0]._id}).
exec( (err, story) => {
if (err) return handleError(err)
person[0].stories.push(story[0])
res.json( { person: person })
})
})
})