Struggling to understand the proper syntax for a set
operation in Mongoose. My schema is structured like this:
const userSchema = mongoose.Schema({
instagram: {
images: [{
id: { type: String },
media_type: { type: String },
media_url: { type: String },
timestamp: { type: Date }
}]
}
});
I need to regularly update this array with the most recent Instagram photos from the user:
User.findOneAndUpdate({ _id }, { $addToSet: { "instagram.images": { $each: arr } } });
How can I ensure that the array always contains the latest images, but never goes over 30 images? Ideally, if there are 20 images in the array and 11 new ones need to be added, all 11 new images should be appended while removing the oldest existing image.