Advertisement
Mushi

Mongo Connection

Aug 15th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var http = require('http');
  2. var MongoClient = require('mongodb').MongoClient;
  3. var url = "mongodb://localhost:27017/mydb";
  4.  
  5. MongoClient.connect(url, function(err, db) {
  6.   if (err) throw err;
  7.   db.createCollection("customers", function(err, res){
  8.     if (err) throw err;
  9.     console.log("Collection created!");
  10.     db.close();
  11.   });
  12. });
  13.  
  14. MongoClient.connect(url, function(err, db) {
  15.   if (err) throw err;
  16.   var myobj = {name: "Company Inc", address: "Highway 37"};
  17.   db.collection("customers").insertOne(myobj, function(err, res){
  18.     if (err) throw err;
  19.     console.log("1 document inserted");
  20.     db.close();
  21.   });
  22. });
  23.  
  24. MongoClient.connect(url, function(err, db) {
  25.   if (err) throw err;
  26.   db.collection("customers").findOne({}, function(err, result){
  27.     if (err) throw err;
  28.     console.log(result.name);
  29.     db.close();
  30.   });
  31. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement