const user = await User.findById(req.user._id);
// finds index of object in the array to be removed
var indexToRemove = -1;
for (var i = 0; i < user.weights.length; i++) {
if (user.weights[i].date === weightInstance.date) {
indexToRemove = i;
}
}
if (indexToRemove > -1) {
// what goes here?
}
user.weights.push(weightInstance);
await user.save();
What i'm trying to do is remove a Weight subdocument object (if it exists) nested inside user (user.weights) that matches in date property with the new Weight subdocument object that I am adding into the user.weights array.
User Schema:
const userSchema = new Schema({
googleId: String,
weights: [Weight]
});
Weight Schema:
const weightSchema = new Schema({
date: String,
weight: Number
});