Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

mongoose export collection

//This code will read the documents from the MongoDB collection 
// (the export functionality) and then writes to a file as JSON. 
// This file is used to read (the import functionality) and 
// insert the JSON into another collection. 
// The code uses MongoDB NodeJS driver.

//Reads from the collection inCollection based upon a supplied query, 
//and writes to a file as JSON "out_file.json".

const MongoClient = require('mongodb').MongoClient;
const fs = require('fs');
const dbName = 'testDB';
const client = new MongoClient('mongodb://localhost:27017', { useUnifiedTopology:true });

client.connect(function(err) {
    //assert.equal(null, err);
    console.log('Connected successfully to server');
    const db = client.db(dbName);

    getDocuments(db, function(docs) {
    
        console.log('Closing connection.');
        client.close();
        
        // Write to file
        try {
            fs.writeFileSync('out_file.json', JSON.stringify(docs));
            console.log('Done writing to file.');
        }
        catch(err) {
            console.log('Error writing to file', err)
        }
    });
}

const getDocuments = function(db, callback) {
    const query = { };  // this is your query criteria
    db.collection("inCollection")
      .find(query)
      .toArray(function(err, result) { 
          if (err) throw err; 
          callback(result); 
    }); 
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: geteliment by id in javascript 
Javascript :: how to create existing nodes in godot 
Javascript :: useEffect : react to manipulate the DOM 
Javascript :: backbone view 
Javascript :: angular service await for data 
Javascript :: how to render react quill the way it is without the html tags 
Javascript :: send form data to endpoint js 
Javascript :: How many options are there to climb a ladder with N 
Javascript :: quill js laravel 
Javascript :: Backbone View In Another View 
Javascript :: radio button remove checked 
Javascript :: jquery loop through model list 
Javascript :: javascript get max value in array of objects 
Javascript :: javascript last value of array 
Javascript :: classes in js 
Javascript :: what is callback hell in javascript 
Javascript :: 2d arrays js 
Javascript :: javascript link to page 
Javascript :: if anagram 
Javascript :: Pause the stream returned by getUserMedia 
Javascript :: strip whitespace from shopify liquid output 
Javascript :: how dynamique pseudo element in react 
Javascript :: javascript variable hoisting 
Javascript :: JavaScript WeakMap Methods 
Javascript :: post css nesting nuxt 
Javascript :: how to write like query in node js 
Javascript :: single page application example javascript 
Javascript :: flip image on y axis phaser 
Javascript :: phaser animation get progress 
Javascript :: NodeJS/express : Cached and 304 status code on chrome 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =