I am working with two collections in my database: Regions and Registrations.
var RegionSchema = new Mongoose.Schema({
name: {type: String},
registrations: [{type: Mongoose.Schema.Types.ObjectId, ref: 'registrations'}],
...
});
The Registration collection is defined as follows:
var RegistrationSchema = Mongoose.Schema({
firstName: {type: String},
...
});
Within my controller, I am creating a new registration and adding it to a region using the upsert
option set to true
:
var registration = new Registration(req.body.registration);
...
Region
.update(
{ _id: user.region},
{ $push: {registrations: registration},
{ upsert: true }
)
.exec();
After executing the code, I noticed that an ObjectId("...")
was added to the registrations property of the region:
{
name: "Northwest",
registrations: [ObjectId("57d038a1466345d52920b194")]
}
However, there was no corresponding document with that _id
in the registrations collection. This led me to question my understanding of the upsert
flag and whether calling save
on the registration object is necessary.