12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- /**
- * Created by root on 2015-07-20.
- */
- var MongoClient = require('mongodb').MongoClient
- , assert = require('assert');
- var database;
- // Connection URL
- var url = 'mongodb://localhost:27017/Behavior';
- // Use connect method to connect to the Server
- module.exports.active = function(callback){
- MongoClient.connect(url, function(err, db) {
- if (!err){
- console.log("Connected correctly to server");
- database = db;
- }
- callback(err);
- });
- };
- module.exports.getCollection = function(name, callback){
- if (!database && !database.connected)
- callback(database.collection(name));
- database.collection(name, {strict:true}, function(err, collection) {
- if (!err)
- callback(err, collection);
- else{
- // Create the collection
- database.createCollection(name, function(err, result) {
- // Retry to get the collection, should work as it's now created
- if (!err)
- database.collection(name, {strict:true}, callback);
- });
- }
- });
- };
|