【发布时间】:2020-07-14 20:51:59
【问题描述】:
我有一个查询,在视频模型中找到,它获取视图,但我需要根据 userId 的视图,例如 userId="1xxxxxxxxx13", 有可能吗?
Video.aggregate(
[
{
$group: {
_id: null,
totalViews: {
$sum: {
$cond: [{ $eq: ["$userId", id] }, "$views", 0],
},
},
},
},
],
function (err, data) {
console.log(data);
return data;
}
);
quer,给我零次观看,但有来自 tht userId 的视频观看次数 这是架构
const mongoose = require("mongoose");
const videoSchema = new mongoose.Schema({
....
thumbnail: { type: String, required: false },
title: { type: String, required: false },
userId: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
views: { type: Number },
....
});
videoSchema.index({ title: "text" });
module.exports = mongoose.model("Video", videoSchema);
我需要过滤特定用户的视频
【问题讨论】:
标签: node.js mongodb mongoose mongodb-query aggregation-framework