我正在为项目创建express服务器,并使用mongodb作为数据库。我得到了错误,
Error validating $lookup value. err=$lookup against an expression value is not allowed in this atlas tier.
电影模式,
const mongoose = require(“mongoose”);
const MovieSchema = new mongoose.Schema(
{
title: { type: String, required: true, unique: true },
desc: { type: String },
img: { type: String },
imgTitle: { type: String },
imgSm: { type: String },
trailer: { type: String },
video: { type: String },
year: { type: String },
limit: { type: Number },
genre: { type: String },
isSeries: { type: Boolean, default: false },
},
{ timestamps: true }
);
module.exports = mongoose.model("movies", MovieSchema);
我的列表架构,
const ListSchema = new mongoose.Schema(
{
title: { type: String, required: true, unique: true },
type: { type: String },
genre: { type: String },
content: [
{ type: mongoose.Schema.Types.ObjectId, required: false, ref: "movies" },
],
},
{ timestamps: true }
);
lists schema
有一个content
属性,它包含movies
的id
。所以我需要在list
中获取所有movies
,sample
大小为4,所以我在list schema
的content
属性中写入aggregation
作为列表模式,用于填充movies对象。我的代码中的bug在哪里?希望对您有所帮助!所以我把它编码为
const list = await List.aggregate([
{ $sample: { size: 4 } },
{
$lookup: {
from: "$movies",
foreignField: "_id",
localField: "content",
as: "content",
},
},
]);
}
res.status(200).json(list);
1条答案
按热度按时间ulydmbyx1#
应该是“电影”而不是“$电影”