mongodb Mongo db错误获取错误验证$lookup值,err=$在此atlas层中不允许对表达式值进行查找

efzxgjgh  于 2023-06-05  发布在  Go
关注(0)|答案(1)|浏览(360)

我正在为项目创建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属性,它包含moviesid。所以我需要在list中获取所有moviessample大小为4,所以我在list schemacontent属性中写入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);
ulydmbyx

ulydmbyx1#

应该是“电影”而不是“$电影”

相关问题