const mongoose = require('mongoose');
const mySchema = new mongoose.Schema({
myArray: [String]
});
const MyModel = mongoose.model('MyModel', mySchema);
const removeItemsByIndexes = async (documentId, indexes) => {
try {
const updateOperations = indexes.map((index) => ({ $pull: { myArray: { $eq: index } } }));
await MyModel.updateOne({ _id: documentId }, updateOperations);
console.log('Items removed successfully.');
} catch (error) {
console.error(error);
}
};
removeItemsByIndexes('documentId', [2, 4, 6]);