我想在用户创建新文档后将文档_id保存到另一个模式的数组中。简而言之:用户保存视频URL,我想将视频的文档_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);
以下是我目前如何设置代码来创建文档并保存到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);
}
});
基本上我不明白如何保存到视频和只将视频文档_id发送到帐户架构中的数组。我该怎么做?
编辑:
尽管已实施建议的修补程序,但data._id
未保存到帐户架构内的数组。不会抛出任何错误。当我使用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);
视频route.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);
});
}
});
有关视频数据未保存到帐户的原因的任何建议?再次感谢。
答案 0 :(得分:1)
刚刚注意到有关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);
});
});
答案 1 :(得分:0)
问题解决了:
我需要在result.save();
之后添加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);
});