【问题标题】:How to keep track of videos watched with Node.js and Mongodb如何跟踪使用 Node.js 和 Mongodb 观看的视频
【发布时间】:2017-04-05 05:25:26
【问题描述】:

我正在构建一个 MEAN 堆栈视频应用程序(我对 Node 和 Mongodb 还很陌生),我需要一种方法来跟踪观看的视频。我该怎么做呢?

我在想我可以在引用视频的用户集合中拥有一组 Id,但我希望能够返回带有 watched: true 键值对的视频,这取决于发出请求的用户。如果这是一个好方法,我如何返回一个依赖于另一个集合中另一个文档的键值对?

用户模型:

let UserSchema = new mongoose.Schema({
email: {
    type: String,
    required: true,
    minlength: 1,
    trim: true,
    unique: true,
    validate: {
        validator: VALUE => validator.isEmail(VALUE),
        message: '{VALUE} is not a valid email'
    }
},
password: {
    type: String,
    required: true,
    minlength: 6
},
admin: {
    type: Boolean,
    default: false
},
vid_inprogress: {
    type: mongoose.Schema.Types.ObjectId
},
vid_completed: [{ type : mongoose.Schema.Types.ObjectId, ref: 'Attachment' }],
tokens: [{
    access: {
        type: String,
        required: true
    },
    token: {
        type: String,
        required: true
    }
}]
});

视频模型:

var Video = mongoose.model('Video', {
url: {
    type: String,
    required: true,
    minlength: 1,
    trim: true
},
title: {
    type: String,
    required: true,
    default: '',
    trim: true
},
description: {
    type: String,
    default: '',
    trim: true
},
img: {
    type: String,
    default: '',
    trim: true
},
attachments: [{ type : mongoose.Schema.Types.ObjectId, ref: 'Attachment' }]
});

vid_completed 在用户模型上是我想跟踪已观看的视频 ID 的地方。 Video 模型是基于是否在用户 vid_completed 数组中找到视频 ID 的键:值对返回的。让我知道这是否有意义。提前致谢!

【问题讨论】:

    标签: node.js mongodb


    【解决方案1】:

    我最终只是在 User 模型中使用了一个数组,如下所示:

    vid_inprogress: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Video'
    },
    vid_completed: [{ type : mongoose.Schema.Types.ObjectId, ref: 'Video' }]
    

    我只需要在前端添加watched: true。如果您有更好的方法,请告诉我。我很想听听。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-05
      • 2015-04-29
      • 2020-12-01
      • 1970-01-01
      相关资源
      最近更新 更多