Consider a schema:
var userSchema = mongoose.Schema({
...
followers: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
following: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }]
...
}
When userA
follows userB
, userB
is to be pushed in userA.following
and userA
is to be pushed in userB.followers
. Both operations require a .save()
.
What is a good way - perhaps conceptual - of ensuring that if either one of the .save()
fails, both documents are left untouched?