Created
December 30, 2021 04:36
-
-
Save catwhocode/9f04944fff96091c7b91de8457d0c805 to your computer and use it in GitHub Desktop.
Connect to MongoDB and insert one data
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 mongodb = require('mongodb') | |
const MongoClient = require('mongodb').MongoClient | |
const url = 'mongodb://localhost:27017/mydb' | |
MongoClient.connect(url, function(err, db){ | |
if (err) throw err; | |
console.log('Connected to mydb') | |
const dbo = db.db('mydb') | |
const user = { | |
username: 'jonpostel', | |
name: 'Jon Postel', | |
email: '[email protected]' | |
} | |
dbo.collection('users').insertOne(user, function(err, result){ | |
if (err) throw err; | |
console.log('1 user inserted') | |
db.close() | |
}) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment