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

374
Vistas
Best way to insert 10,000 JSON files (total 30GB) into MongoDB

It doesn't look to me that using python to combine all the json files is convenient, and the combined file would be 30G.

I am using mongoDB and nodejs. The way how I populate a sample json is:

var data = require('./data1.json')
var populateDB = function() {
   db.collection('temp', function(err, collection) {
    collection.insert(data, {safe:true}, function(err, result) {});
 });
};

This only adds one json file. How should I populate the collection with the 10000+ json files from here? any suggestion is highly appreciated!

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

0

The easiest way would be to write a Node program that processes one JSON file and then run it multiple time from the shell:

for i in *.json; do node program.js $i; done

Your Node program would just need to access the name from process.argv instead of having it hardcoded but the logic would be the same.

If you want to do everything in node then you will have to read the directory, get all .json files, read every one of them in sequence and then run a code similar to the one you posted. If it's a one off task then you can even get away with using the "Sync" functions to simplify your code if it's a sequential task to do one thing at a time and you don't care about adding the data in parallel.

over 4 years ago · Santiago Trujillo Denunciar

0

Something like this would work

npm i glob-fs mongodb async --save

const async = require('async');
const fs = require('fs');
const glob = require('glob-fs')({ gitignore: true });
const MongoClient = require('mongodb').MongoClient;

const files = './files/data*.json';
const collection = 'test';
const url = 'mongodb://localhost:27017/test';

// Connect to db
MongoClient.connect(url, function (err, db) {
  if (err) {
    console.log(err);
  }

  // Get the collection
  const col = db.collection(collection);

  glob.readdirPromise(files)
    .then(function (f) {
      return async.eachSeries(f, (item, callback) => {

        fs.readFile(item, 'utf8', function (err, data) {
          if (err) {
            return console.log(err);
          }

          // Insert into mongo
          col.insertMany(JSON.parse(data)).then((r) => {
            console.log(r);
            return callback(r);
          }).catch(function (fail) {
            console.log(fail)
          });

        });
      }, err => {
        console.log(err);
      });
    })
    .then(err => {
      if (err) {
        db.close();
      }
    })
    .catch(err => {
      console.log(err);
    });
});
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