Trying to figure out the exact syntax for a set operation in Mongoose. I have a schema that looks like this:
const userSchema = mongoose.Schema({
instagram: {
images: [{
id: { type: String },
media_type: { type: String },
media_url: { type: String },
timestamp: { type: Date }
}]
}
});
I will periodically update this array with the user's latest Instagram photos like so:
User.findOneAndUpdate({ _id }, { $addToSet: { "instagram.images": { $each: arr } } });
How can I make sure the array is updated with the latest images, but never exceeds 30 images? The functionality should be that if the array has 20 images, and there are 11 new images to add, then all 11 new images should be added to the array and the last item that currently exists in the array should be removed.