I have schemas countrySchema
and citySchema
citySchema = {
cityName: String,
schools: [schoolSchema]
}
countrySchema = {
countryName: String,
cities: [citySchema]
}
I want to push a school to the schools
array in the city schema.
I already know the countryId
and cityId
, so I guess it's something like
Country.findByIdAndUpdate(countryId, { $push: { 'cities.schools': school } }, (err, country) => {
//
});
but this won't work since I don't specify which city the school belongs to.
How should the query be? Maybe it could also be
Country.findOneAndUpdate({ _id: countryId, 'cities._id': cityId }, { $push: { 'cities.schools': school } }, (err, country) => {
//
});