Last active
April 23, 2024 17:24
-
-
Save polsala/7f78f420c0a8f7de5187d505ef08e8f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Conexión a MongoDB | |
DATABASE="tuBaseDeDatos" | |
COLLECTION="tg_profile" | |
MONGO_HOST="localhost" | |
MONGO_PORT="27017" | |
# Obtener el mínimo y máximo _id | |
MIN_ID=$(mongo --quiet --host $MONGO_HOST --port $MONGO_PORT $DATABASE --eval "printjson(db.$COLLECTION.find().sort({_id: 1}).limit(1).toArray()[0]._id)") | |
MAX_ID=$(mongo --quiet --host $MONGO_HOST --port $MONGO_PORT $DATABASE --eval "printjson(db.$COLLECTION.find().sort({_id: -1}).limit(1).toArray()[0]._id)") | |
# Configuración de lotes | |
BATCH_SIZE=100000 | |
CURRENT_ID=$MIN_ID | |
# Bucle para actualizar en lotes | |
while [[ "$CURRENT_ID" < "$MAX_ID" ]] | |
do | |
# Marcar tiempo de inicio | |
START_TIME=$(date +%s) | |
# Comando de actualización | |
mongo --host $MONGO_HOST --port $MONGO_PORT $DATABASE --eval "db.$COLLECTION.updateMany({_id: {\$gte: ObjectId('$CURRENT_ID'), \$lt: ObjectId('$NEXT_ID')}}, {\$set: {type: 'p'}})" | |
# Calcular el próximo _id para el lote | |
NEXT_ID=$(mongo --quiet --host $MONGO_HOST --port $MONGO_PORT $DATABASE --eval "printjson(db.$COLLECTION.find({_id: {\$gt: ObjectId('$CURRENT_ID')}}).sort({_id: 1}).limit($BATCH_SIZE).toArray().slice(-1)[0]._id)") | |
# Marcar tiempo de finalización y calcular la duración | |
END_TIME=$(date +%s) | |
DURATION=$((END_TIME - START_TIME)) | |
echo "Lote desde $CURRENT_ID hasta $NEXT_ID actualizado en $DURATION segundos." | |
# Actualizar el ID actual para el próximo lote | |
CURRENT_ID=$NEXT_ID | |
done | |
# Crear índice para el campo 'type' | |
echo "Creando índice para el campo 'type'..." | |
mongo --host $MONGO_HOST --port $MONGO_PORT $DATABASE --eval "db.$COLLECTION.createIndex({type: 1})" | |
echo "Índice creado." | |
echo "Actualización completada." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment