简体   繁体   English

猫鼬模式,无效的模式配置

[英]mongoose schema , Invalid schema configuration

i have a schema of user that contain an array of movie i create a new schema of Movie but i get error of invalid schema configuration.我有一个包含电影数组的用户架构 我创建了一个新的电影架构,但我收到无效架构配置的错误。 what i'm doning worng?我在做什么?

enter code here
const mongoose = require("mongoose");
const Movie = require("./movie-model");

const Schema = mongoose.Schema;

const User = new Schema({
  firstName: { type: String, required: true },
  lastName: { type: String, required: true },
  email: { type: String, required: true },
  password: { type: String, required: true },
  movies: { type: Movie },
});

module.exports = mongoose.model("user", User);
var mongoose = require("mongoose");
var Schema = mongoose.Schema;

var Movie = new Schema(
  {
    name: { type: String, required: true },
    time: { type: String, required: true },
    rating: { type: Number, required: false },
    priorety: { type: Number, required: true },
  },
);

module.exports = mongoose.model("movie", Movie);

in movie-model在电影模型中

module.export = Movie module.export = 电影

instead of代替

module.exports = mongoose.model("movie", Movie); module.exports = mongoose.model("电影", 电影);

User has many movies用户有很多电影

const mongoose = require("mongoose");
const Movie = require("./movie-model");

const Schema = mongoose.Schema;

const User = new Schema({
  firstName: { type: String, required: true },
  lastName: { type: String, required: true },
  email: { type: String, required: true },
  password: { type: String, required: true },
  movies: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "movie",
    },
  ],
});

module.exports = mongoose.model("user", User);

I invite you to read more about One-to-Many Relationships with Embedded Documents我邀请您阅读有关嵌入文档的一对多关系的更多信息

You need to done referencing in User collection您需要在用户集合中完成引用

movies: [
type: mongoose.Schema.ObjectId,
ref: 'user'
]
//instead of doing that
movies: { type: Movie },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM