2016-09-05 123 views
0

我正在努力尋找貓鼬兒童人口的例子或文件。貓鼬子女

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); 
    }); 
}) 

所以這就是我在哪裏會推動t他'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

你叫assignment.save(); ? – Molda

+0

@Molda對不起,我沒有複製那部分 –

回答

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陣列;-)