埋め込まれたドキュメントがそれぞれのコレクションに保存されていないようです。これが私のモデルです:
var County = new Schema({
_id : Schema.ObjectId,
name : String,
biggestCity : String
});
var Country = new Schema({
_id : Schema.ObjectId,
name : String,
counties : {type: [County], ref: "County"}
});
var Continent = new Schema({
_id : Schema.ObjectId,
countries : {type: [Country], ref: "Country"},
});
...そして、MongoDBに保存するために使用するコードは次のとおりです。
var continentModel = mongoose.model("Continent");
var continent = new continentModel();
country.name = name;
var countryModel = mongoose.model("Country");
var countyModel = mongoose.model("County");
for (var i = 0; i < req.body.countries.length; i++) {
var country = new countryModel();
country.name = req.body.countries[i].name;
for (var j = 0; j < req.body.countries[i].counties.length; j++) {
var county = new countyModel();
county.name = req.body.countries[i].counties[j].name;
county.biggestCity = req.body.countries[i].counties[j].biggestCity;
countries.counties.push(county);
}
continent.countries.push(country;
}
continent.save();
db.continents.find()を実行すると、すべてのプロパティ(国と郡を含む)が入力されたドキュメントが返されます。
しかし、db.counties.find()またはdb.countries.find()を実行しても、何も返されません。したがって、CountyおよびCountryドキュメントはDBにそれぞれのコレクションに保存されているのではなく、代わりに通常のプロパティとして(埋め込みドキュメントではなく)Continentコレクションに保存されているように見えます。
私は何が間違っているのですか?