Skip to content

Instantly share code, notes, and snippets.

@skobba
Created January 29, 2020 21:35
Show Gist options
  • Save skobba/8dcbf33732b5e90fd86439d4489950e0 to your computer and use it in GitHub Desktop.
Save skobba/8dcbf33732b5e90fd86439d4489950e0 to your computer and use it in GitHub Desktop.
mongodb instert document javascript
const MongoClient = require('mongodb').MongoClient; // eslint-disable-line import/no-extraneous-dependencies
// Database Name
const dbName = 'demo';
(async () => {
const client = await MongoClient.connect(
'mongodb://mongouser:mongopass@localhost:27017/demo?authSource=demo'
);
console.log('Connected successfully to server'); // eslint-disable-line no-console
const db = client.db(dbName);
const collection = db.collection('test');
const doc1 = { hello: 'doc1' };
const doc2 = { hello: 'doc2' };
const lotsOfDocs = [{ hello: 'doc3' }, { hello: 'doc4' }];
collection.insert(doc1);
collection.insert(doc2, { w: 1 }); // callback: function(err, result) {}
collection.insert(lotsOfDocs, { w: 1 });
client.close();
return;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment