我正在为该项目创建 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
属性,它包含id
的movies
。所以我需要获得movies
中的所有list
,sample
大小为4,所以我写 aggregation
用于填充 content
中的 list schema
属性中的电影对象的列表架构。那么我的代码中的错误在哪里?我希望你的帮助!
所以我将其编码为,
const list = await List.aggregate([
{ $sample: { size: 4 } },
{
$lookup: {
from: "$movies",
foreignField: "_id",
localField: "content",
as: "content",
},
},
]);
}
res.status(200).json(list);
应该是“电影”而不是“$movies”。 更正后的代码将是:
const list = await List.aggregate([
{ $sample: { size: 4 } },
{
$lookup: {
from: "movies",
foreignField: "_id",
localField: "content",
as: "content",
},
},
]);
}
res.status(200).json(list);