Skip to content

Instantly share code, notes, and snippets.

@flipjs
Created August 6, 2014 09:37
Show Gist options
  • Save flipjs/121599cbe8834fb44cd7 to your computer and use it in GitHub Desktop.
Save flipjs/121599cbe8834fb44cd7 to your computer and use it in GitHub Desktop.
MongoDB and NodeJS
var mongo = require('mongodb')
var Server = mongo.Server
, Db = mongo.Db
, BSON = mongo.BSONPure
var server = new Server('localhost', 27017, {auto_reconnect: true})
, db = new Db('dbName', server)
db.open(function(err, db) {
if (!err) {
console.log('Connected to "dbName" database')
db.collection('collName', {strict: true}, function(err, collection) {
if (err) {
// if collName collection does not exist,
// do something about it here (i.e, recreate one, return err, etc.)
}
})
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment