Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

858
Vistas
mongodb import multiple collections at once

I am using this command

mongoimport --db databasename

to import a database that I exported using mongoexport. The database has over 100 collections in the mongoimport documentation you need to specify the collection name and json file. How do I go about importing all the collections at once without having to type a command for each collection

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

If dump files are in .json you can use the script for import

ls *.json | sed 's/.metadata.json//' | while read col; do mongoimport -d db_name -c $col < $col.metadata.json; done

If dump files are in .json.gz you can use the script for import

ls *.gz | sed 's/.metadata.json.gz//' | while read col; do mongoimport -d db_name --gzip -c $col < $col.metadata.json.gz; done
over 4 years ago · Santiago Trujillo Denunciar

0

According to the documentation

New in version 2.6: If you do not specify --collection, mongoimport takes the collection name from the input filename. MongoDB omits the extension of the file from the collection name, if the input file has an extension.

So it seems it is supposed to do import one collection at a time. So unless you write a shell script to do it, I don't see a way.

mongodump and mongorestore are much better for taking full db dump and restore it at once as the other part of the same document says

Warning

Avoid using mongoimport and mongoexport for full instance production backups. They do not reliably preserve all rich BSON data types, because JSON can only represent a subset of the types supported by BSON. Use mongodump and mongorestore as described in MongoDB Backup Methods for this kind of functionality.

over 4 years ago · Santiago Trujillo Denunciar

0

It is very surprising that there isn't easy to find documentation on importing multiple collections. Restoring a database isn't always intuitive keyword when you are searching for it, but that is what you want to do when you want to restore a database from a backup. If you already have a dump of collections that you exported with mongodump, you should be able to use mongorestore.

This is all you really need to run:

mongorestore --db db_name ./db_dumpfiles/
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda