Created
January 29, 2020 21:35
-
-
Save skobba/8dcbf33732b5e90fd86439d4489950e0 to your computer and use it in GitHub Desktop.
mongodb instert document javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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