mongoose分开的文件,而embedded架构?
我想在用户创build一个新文档之后将一个文档_id保存到另一个模式的数组中。 简而言之:用户保存一个videoURL,我想将video的文档_id保存到用户架构中的一个数组中。 我很难搞清楚如何做到这一点。 这里是我的模型文件:
videos.js:
// Video Model // ------------- var mongoose = require('mongoose'); var Schema = mongoose.Schema; // Embedded document schema var NotesSchema = new Schema({ timecode : String, text : String }); // Main schema var VideoSchema = new Schema({ title : String, url_id : String, notes : [NotesSchema] }); module.exports = mongoose.model('Note', NotesSchema); module.exports = mongoose.model('Video', VideoSchema);
account.js:
// Account Model // ------------- var mongoose = require('mongoose'); var Schema = mongoose.Schema; var Video = require('../models/videos.js'); var passportLocalMongoose = require('../node_modules/passport-local-mongoose/lib/passport-local-mongoose.js'); var AccountSchema = new Schema({ username: String, salt: { type: String, required: true }, hash: { type: String, required: true }, videos: [VideoSchema] // <- need to save here }); AccountSchema.plugin(passportLocalMongoose); module.exports = mongoose.model('Account', AccountSchema);
以下是我目前如何创build文档并保存到MongoDB的代码设置。
var video = new Video({ title : req.body.title, url_id : req.body.url_id }); video.save(function(err) { if (err) { console.log(err); } else { res.redirect('videos/' + video._id); console.log('video saved.'); console.log('video information: ' + video); } });
基本上我不明白如何保存到video,只发送video文件_id帐户架构中的数组。 我该怎么做呢?
编辑:
尽pipe实现了build议的修复, data._id
不保存到Account模式内的数组。 没有错误被抛出。 当我使用mongo CLI检查帐户时,数组是空的。
这是我目前的变化:
的Video.js
// Video Model // ------------- var mongoose = require('mongoose'); var Schema = mongoose.Schema; var NotesSchema = new Schema({ timecode : String, text : String }); var VideoSchema = new Schema({ title : String, url_id : String, notes : [NotesSchema] }); module.exports = mongoose.model('Note', NotesSchema); module.exports = mongoose.model('Video', VideoSchema);
account.js
// Account Model // ------------- var mongoose = require('mongoose'); var Schema = mongoose.Schema; var Video = require('../models/videos'); var passportLocalMongoose = require('../node_modules/passport-local-mongoose/lib/passport-local-mongoose.js'); var AccountSchema = new Schema({ nickname: String, birthday: Date, videos: [{ type: Schema.Types.ObjectId, ref: 'Video' }] }); AccountSchema.plugin(passportLocalMongoose); module.exports = mongoose.model('Account', AccountSchema);
videoroute.js
var util = require('util'); var mongoose = require('mongoose'); var Video = require('../models/videos'); var Account = require('../models/account'); var video = new Video({ title : req.body.title, url_id : goodVimeoId }); video.save(function(err, data) { if (err) { console.log(err); } else { Account.findOne({username : req.user.username}, function(err, result) { result.videos.push(video._id); res.redirect('videos/' + video._id); }); } });
有关为什么video数据没有保存到帐户的任何build议? 再次感谢。
刚刚注意到关于ObjectId的评论。 根据需要更新了答案:
var ObjectId = Schema.ObjectId; var AccountSchema = new Schema({ username: String, salt: { type: String, required: true }, hash: { type: String, required: true }, videos: [{type: ObjectId, ref: 'Video'}] // <- need to save here }); .... .... video.save(function(err, data) { Account.findOne({username:req.body.username}, function(err, result) { result.videos.push(data._id); }); });
问题解决了:
我需要添加result.save();
after result.videos.push(video._id);
如下所示:
Account.findOne({username : req.user.username}, function(err, result) { result.videos.push(video._id); result.save(); res.redirect('videos/' + video._id); });