Essentially, I have a User that has an email and a list of subscriptions. I want to create the User with the subscription if the user doesn’t exist, if the user exists but doesn’t have the subscription I Want to simply add the newSubscription to the Users list of subscriptions. Finally, if both User and subscription already exist then I want to handle the existing subscription in my own way.
await UserDocument.findOneAndUpdate(
{'$and': [{
'email': email,
}, {
'subscriptions.product': product-id,
}]},
{'$setOnInsert': newCustomerWithSubscription,
'$push': {'email.$.subscription': newSubscription}},
{upsert: true, new: false},
async function(err: CallbackError, doc: UserSchemaType | null) {
//Code to handle if both exists
}).exec();
DB Design:
User: {
email,
subscriptions: [{
//sub-info
}]
}