将对象数组插入文档

时间:2018-06-05 18:12:18

标签: node.js mongodb

我试图将包含有关电影数据的文档存储到我的数据库中。

我从JSON格式的API获取数据。

{"Title":"Black Panther","Year":"2018","Rated":"PG-13","Released":"16 Feb 2018","Runtime":"134 min","Genre":"Action, Adventure, Sci-Fi","Director":"Ryan Coogler","Writer":"Ryan Coogler, Joe Robert Cole, Stan Lee (based on the Marvel Comics by), Jack Kirby (based on the Marvel Comics by)","Actors":"Chadwick Boseman, Michael B. Jordan, Lupita Nyong'o, Danai Gurira","Plot":"T'Challa, the King of Wakanda, rises to the throne in the isolated, technologically advanced African nation, but his claim is challenged by a vengeful outsider who was a childhood victim of T'Challa's father's mistake.","Language":"Swahili, Nama, English, Xhosa, Korean","Country":"USA","Awards":"14 nominations.","Poster":"https://ia.media-imdb.com/images/M/MV5BMTg1MTY2MjYzNV5BMl5BanBnXkFtZTgwMTc4NTMwNDI@._V1_SX300.jpg","Ratings":[{"Source":"Internet Movie Database","Value":"7.7/10"},{"Source":"Rotten Tomatoes","Value":"97%"},{"Source":"Metacritic","Value":"88/100"}],"Metascore":"88","imdbRating":"7.7","imdbVotes":"247,680","imdbID":"tt1825683","Type":"movie","DVD":"15 May 2018","BoxOffice":"$501,105,037","Production":"Marvel Studios","Website":"https://www.facebook.com/BlackPantherMovie/","Response":"True"}

有了这些数据,我可以访问除数据

之类的收视率以外的所有字段
exports.addFilm = function (req, res) {
console.log(req.body);

data = req.body;

var film = new Film({
   title: data.Title,
   year: data.Year,
   rated: data.Rated,
   released: data.Released,
   runtime: data.Runtime,
   genre: data.Genre.split(","),
   director: data.Director,
   writer: data.Writer.split(","),
   actors: data.Actors.split(","),
   plot: data.Plot,
   language: data.Language.split(","),
   country: data.Country,
   poster: data.Poster,
   ratings: data.Ratings,
   imbdID: data.imdbID,
   production: data.Production,
   website: data.Website,
});

film.save(function (err, results) {
    console.log(results._id);
    res.json(results);
});

}

在代码中,除了data.Ratings之外,其余的工作都是未定义的。如何在我的数据库中将三个元素存储在评级中?

0 个答案:

没有答案