Skip to content

Instantly share code, notes, and snippets.

@Debdut
Created August 21, 2017 20:26
Show Gist options
  • Save Debdut/6c87dbfc806a67c064b36baa069e18ac to your computer and use it in GitHub Desktop.
Save Debdut/6c87dbfc806a67c064b36baa069e18ac to your computer and use it in GitHub Desktop.
var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/testdb";
var companies = [{'name' : 'Sun Pharma', 'medicine' : [1, 2]},
{'name' : 'Hello Pharma', 'medicine' : [3, 4]},
{'name' : 'Sayan Pharma Ltd.', 'medicine' : [5]}];
MongoClient.connect(url, (err, db) => {
if (err) {
console.log (err);
} else {
db.createCollection ('company', (err, collection) => {
var index = 0;
companies.forEach ((company) => {
collection.insert (company, () => {
if (index++ === companies.length-1)
{
collection.find ().toArray ((err, result) => {
db.close (() => {
console.log (result);
});
});
};
});
});
});
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment