【问题标题】:How can I implement this model我怎样才能实现这个模型
【发布时间】:2020-02-07 05:14:47
【问题描述】:

我该如何实现这个

[{
    "title": "pranam",
    "year": "2016",
    "rating": 9,
    "actors": [
        {
            "name": "Amir",
            "birthday": "16 Aug 1982",
            "country": "Bangladesh"
        },
        {
            "name": "Imran",
            "birthday": "15 Aug 1982",
            "country": "Bangladesh"
        }
    ]
}]

我试过这个......

模型/actors.js

const Joi = require('joi');
const mongoose = require('mongoose');


const actorSchema = new mongoose.Schema({
    name:{
        type: String,
        required: true,
        min: 5,
        max:50
     },
    birthday:{
        type: String,
        required: true
    },
    country:{
        type: String,
        required: true
    }


 });

models/movies.js

const mongoose = require('mongoose');
const Joi = require('joi');
const actorSchema = require('../models/actors');


const movieSchema = new mongoose.Schema({
    title:{
    type:String,
    required:true,
    min: 5,
    max: 50
},
year:{
    type: String,
    required: true,
    min:2,
    max:4
},
rating:{
    type: Number,
    required: true,
    min:0,
    max:10
},
actors: {
    type: actorSchema,
    required: true
}

});   

routes/movies.js

const { Movie, validate} = require('../models/movies');
const { Actor} = require('../models/actors');
const auth = require('../middleware/auth');
const router = express.Router();


router.get('/', auth, async(req, res)=>{
    const movies = await Movie
    .find({}, { _id:0, __v:0 })
    res.send(movies);
 });  

router.post('/', async(req, res)=>{
   const {error} = validate(req.body);
   if(error) return res.status(400).send(error.details[0].message)

 //May be problem is hare, But I can not solve 
 const actor = await Actor.findById(req.body.actorId);
 if(!actor) return res.status(400).send('Invalid Actors');



 let movie = new Movie({

     title: req.body.title,
     year: req.body.year,
     rating: req.body.rating,
     actors:[{
        name: actor.name,
        birthday: actor.birthday,
        country: actor.country
     }]   
 });


 try {
     movie = await movie.save();
     res.send(movie)
   } catch (ex) {
     console.log("Invalid Movie ");
   }
 });
module.exports =router;

我在邮递员中通过 POST 方法输入 {“标题”:“我讨厌爱情故事”,“评分”:“9”,“actorId”:[“5d99ac95f17917117068631b”,“5d99ad75c4edd61f98af740b”] } 这仅显示 GET api 调用输出的电影中的第一个演员数据, 如何在电影中显示更多演员数据。

【问题讨论】:

    标签: node.js express mongoose model mongoose-schema


    【解决方案1】:

    在问题的上下文中,其他一切看起来都很好,直到在 routes/movies.js 中的这一点:

    const actor = await Actor.findById(req.body.actorId);
    

    我认为查询不正确,首先,Model.findById() 只接受一个 id 而不是一个 id 数组。其次,您在这里要做的是获取由 actorId 数组中的 id 标识的所有演员,这是一个有效的查询:

    const actors = await Actor.find(
      // Filter: fetch all actors whose Id is in the array of ids provided in the request
      { _id: { $in: req.body.actorId } }, 
      // Projection: Include just the name, birthday and country in the response, if any.
      { name: 1, birthday: 1, country: 1 } 
    );
    

    您可以查看Model.find() 以获取有关如何使用其查询界面的更多信息。
    上面的查询应该返回多个演员,这就是你所需要的,然后你可以用它实例化一个新的电影模型:

     new Movie({
       title: req.body.title,
       year: req.body.year,
       rating: req.body.rating,
       actors,
     }); 
    

    【讨论】:

    • 不客气。还有一件事,我认为重要的是我指出演员数据存储在电影对象中的方式不是规范化,例如,如果出于某种原因需要要更新演员名称,您可能需要在 Actors 和 Movies 集合中更新它,无论如何,我认为这会很痛苦,这取决于您的实现。
    猜你喜欢
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 2012-11-21
    • 2015-04-18
    • 1970-01-01
    相关资源
    最近更新 更多