I am working on a code snippet for my application
const citySchema = new Schema({
cityName: {
type: String,
required: true,
},
citizen:[{
type: Schema.Types.ObjectId,
ref: "Citizen",
}],
});
module.exports = mongoose.model("City", citySchema);
const citizenSchema = new Schema({
citizenName: {
type: String,
required: true,
},
city:{
type: Schema.Types.ObjectId,
ref: "City",
},
});
module.exports = mongoose.model("Citizen", citizenSchema);
router.post('/', (req, res) => {
// req.body.cityName
// req.body.citizenName
})
When making a POST request, I receive both the city name (for a new city) and citizen name (for a new citizen) that are not already in the database. However, I want to ensure that both these schemas are updated correctly by:
- Ensuring City contains references to Citizens
- Ensuring Citizen contains reference to the City
How can I achieve this? Your assistance would be much appreciated.