我是新來的mongo和貓鼬。我需要從這個對象創建貓鼬模式並插入文檔
trailers: [
{
id: 310131,
results: [
{
id: "564b9086c3a3686026004542",
iso_639_1: "en",
key: "gy_-pupKvxg",
name: "The Witch Official Trailer 1 2016 HD",
site: "YouTube",
size: 1080,
type: "Trailer"
},
{
id: "56aab2b19251417e110008b2",
iso_639_1: "en",
key: "feRupW34rUU",
name: "Trailer 2",
site: "YouTube",
size: 1080,
type: "Trailer"
}
]
}
],
這裏創建架構如何,我此刻
// My Schema
var movieSchema = new mongoose.Schema({
....
...
..
.,
trailers : [
{
id: Number,
results : [
{
id: String,
iso_639_1: String,
key: String,
name: String,
site: String,
size: Number,
type: String,
}
],
_id : false
}
],
....
..
.
});
var Movie = mongoose.model('Movies', movieSchema);
// Insert movie into schema and save to database
movies.forEach(function(m) {
var movie = new Movie({
....
...
..
.,
trailers : m.trailers ? m.trailers : "",
....
...
..
.
});
movie.save(function(err) {
if(err) console.log('Error on save' + err);
});
}, this);
這樣做,但我做錯了任何人都可以當場或者告訴我如何將每部電影進入模式對象。提前致謝。
'_id:false'你在做什麼這樣做? –
它可以防止id在對象上的自動生成...我認爲 – jonjonson
你從哪裏閱讀過這些內容? _id是MongoDB中文檔中總是存在的東西。如果你提供一個,它不會自己創建一個。但是如果你沒有MongoDB爲你創建它。 –