Skip to content

Instantly share code, notes, and snippets.

@stevenspads
Last active July 19, 2016 04:15
Show Gist options
  • Save stevenspads/6cd900ca773ca01cda32d19e9dc5f195 to your computer and use it in GitHub Desktop.
Save stevenspads/6cd900ca773ca01cda32d19e9dc5f195 to your computer and use it in GitHub Desktop.
//1. Install mongodb package for Express
$ sudo npm install —-save mongodb
//--save will add mongodb to the Express dependencies in package.json
//2. Now to set up MongoDB usage in your router (ex: routes/index.js):
var express = require('express');
var router = express.Router();
var mongo = require('mongodb').MongoClient;
var objectId = require('mongodb').ObjectID;
//mongodb is strongly typed and uses ObjectIDs as IDs. IDs as strings wont work for DB update or delete. We need to use ObjectID.
var assert = require('assert');
//assert is built into node.js for testing and comparing values. It's useful to check if db transactions succeeded or not
var url = 'mongodb://localhost:27017/test'; //test is the name of the MongoDB database and 27017 is the default MongoDB port
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment