Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nodejs export all mongodb collections

const MongoClient = require('mongodb').MongoClient;
const fs = require('fs');
const dbName = 'database1';
const client = new MongoClient('db_url', {useUnifiedTopology:true,useNewUrlParser: true, });

client.connect(function(err) {
  console.log('Connected successfully to server');
  const db = client.db(dbName);
  var collections = [ 'collection1', 'collection2' ];
  collections.forEach(async collection => {
    var documents =  await getDocuments(db, collection);
    try {
      // Write files outside of server directory
      // prevents app restarts on nodemon
      fs.writeFile("../"+collection+'.json', JSON.stringify(docus), err => {
      });
      console.log('Done writing to file.');
    } catch (err) { 
      console.log('Error writing to file', err)
    }
  })
});

async function getDocuments(db, collection) {
  return await db.collection(collection).find({}).toArray()
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: set to array casting js 
Javascript :: $("#id").submit in vanilla 
Javascript :: js filter method 
Javascript :: jquery-3.5.0.min.js 
Javascript :: html js display pdf file 
Javascript :: check if specific letter exist in string javascript 
Javascript :: loop in javascript 
Javascript :: Get Input arrays 
Javascript :: how to migrate data from one elasticsearch to another 
Javascript :: supertest expect content type 
Javascript :: link externo no react 
Javascript :: react native notify user for new version of app 
Javascript :: js string to charcode array 
Javascript :: run javascript in flutter 
Javascript :: react native on refresh change color flat list 
Javascript :: Sequelize plain result 
Javascript :: using filter and pipe in rxjs 
Javascript :: programmatically create a custom cron job drupal 7 
Javascript :: emergency food meme 
Javascript :: nuxt history back 
Javascript :: javascript get first entry from set 
Javascript :: slice javascript 
Javascript :: how to test usehistory in jest 
Javascript :: associative array add new key and value js 
Javascript :: vue slice words 
Javascript :: load all icon from a folder in react 
Javascript :: version check 
Javascript :: loadstring json flutter 
Javascript :: find when webpage was last updated js 
Javascript :: JSON schema enumerated type 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =