node.js - Mongoose 在嵌入模式时分离文档?

标签 node.js mongodb express mongoose

我想在用户创建新文档后将文档 _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 检查帐户时,数组为空。

以下是我当前的更改:

视频.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);

视频路由.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);
    });

  }
});

对于视频数据未保存到帐户的原因有什么建议吗?再次感谢。

最佳答案

刚刚注意到关于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);
     });
  });

关于node.js - Mongoose 在嵌入模式时分离文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15352286/

相关文章:

javascript - 检查字符串是否与日期匹配?

javascript - MarkLogic - JavaScript node.js 客户端 API - QueryBuilder - 集合之间的连接

javascript - Mongodb - 错误查询 : BadValue unknown top level operator: $gte

node.js - 带有嵌套可选对象和必填字段的 Mongoose 模式

javascript - 如何将 API 响应作为 JSON 对象发送给客户端?

javascript - 使用nodejs将文件保存到外部服务器

node.js - Express 中间件 protected 和不 protected 路由

javascript - 使用指针查找 Mongoose 对象

javascript - 使用 ExpressJS 的请求模块在所有传出的 http 请求中添加 header ,可能吗?

javascript - Express js路由错误("Cannot GET/")