//node es5
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("mydb");
//Create a collection name "customers":
dbo.createCollection("customers", function(err, res) {
if (err) throw err;
console.log("Collection created!");
db.close();
});
});
//for express ES6
import * as mongodb from 'mongodb';
import { MongoClient } from 'mongodb'
const url ="mongodb://localhost:27017/machinetest";
MongoClient.connect(url,(err,db)=>{
//console.log(db);
if(err) throw err ;
var dbo = db.db('vasu');
const query = {name: req.body.name, email:req.body.email, password:req.body.password, mobile:req.body.mobile};
dbo.collection("register").insertOne(query,(err,result)=>{
if(err) throw err;
console.log(result);
res.send('Inserted ID : '+result.insertedId);
db.close();
})
});
Comments
Post a Comment