I would like to save a document _id to another schema's array after a user creates a new document. In short: a user saves a video URL, and I would like to save the video's document _id to an array in the user schema. I am having trouble figuring out how to do this. Here are my model files:
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);
Here's how I currently have the code setup to create a document and save to 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);
}
});
Basically I don't understand how to save to video and only send the video document _id to the array in the account schema. How do I do this?
EDIT:
Despite implementing the suggested fixes, data._id is not saving to the array inside the Account schema. No error is thrown. When I check on a account using the mongo CLI, the array is empty.
Here are my current changes:
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);
video-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);
});
}
});
Any suggestions on why the video data is not saving to the account? Thanks again.
Just noticed the comment about ObjectId. Have updated the answer as required:
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);
});
});
Problem solved:
I need to add result.save(); after result.videos.push(video._id);, like so:
Account.findOne({username : req.user.username}, function(err, result) {
result.videos.push(video._id);
result.save();
res.redirect('videos/' + video._id);
});