diff --git a/lib/server/entries.js b/lib/server/entries.js index d7258f13b79..02dd115bb95 100644 --- a/lib/server/entries.js +++ b/lib/server/entries.js @@ -140,7 +140,15 @@ function storage(env, ctx) { api.query_for = query_for; api.getEntry = getEntry; api.aggregate = require('./aggregate')({ }, api); - api.indexedFields = [ 'date', 'type', 'sgv', 'mbg', 'sysTime', 'dateString' ]; + api.indexedFields = [ + 'date' + , 'type' + , 'sgv' + , 'mbg' + , 'sysTime' + , 'dateString' + , { 'type' : 1, 'date' : -1, 'dateString' : 1 } + ]; return api; } @@ -160,4 +168,3 @@ storage.queryOpts = { // expose module storage.storage = storage; module.exports = storage; - diff --git a/lib/server/treatments.js b/lib/server/treatments.js index 580e5cf0c45..3edd00a155e 100644 --- a/lib/server/treatments.js +++ b/lib/server/treatments.js @@ -133,6 +133,7 @@ function storage (env, ctx) { , 'percent' , 'absolute' , 'duration' + , { 'eventType' : 1, 'duration' : 1, 'created_at' : 1 } ]; api.remove = remove; diff --git a/lib/storage/mongo-storage.js b/lib/storage/mongo-storage.js index 275cde6eb8a..fbbc328d0e2 100644 --- a/lib/storage/mongo-storage.js +++ b/lib/storage/mongo-storage.js @@ -37,11 +37,11 @@ function init (env, cb, forceNewConnection) { console.log('Error connecting to MongoDB: %j - retrying in ' + timeout/1000 + ' sec', err); setTimeout(connect_with_retry, timeout, i+1); } else if (err.message) { - throw new Error('MongoDB connection string '+env.storageURI+' seems invalid: '+err.message) ; + throw new Error('MongoDB connection string '+env.storageURI+' seems invalid: '+err.message) ; } } else { console.log('Successfully established a connected to MongoDB'); - + var dbName = env.storageURI.split('/').pop().split('?'); dbName=dbName[0]; // drop Connection Options mongo.db = client.db(dbName); @@ -66,7 +66,7 @@ function init (env, cb, forceNewConnection) { mongo.ensureIndexes = function ensureIndexes (collection, fields) { fields.forEach(function (field) { console.info('ensuring index for: ' + field); - collection.ensureIndex(field, function (err) { + collection.createIndex(field, { 'background': true }, function (err) { if (err) { console.error('unable to ensureIndex for: ' + field + ' - ' + err); } diff --git a/testing/populate.js b/testing/populate.js index 34c307b7759..c4d9e1422a1 100644 --- a/testing/populate.js +++ b/testing/populate.js @@ -3,19 +3,22 @@ var mongodb = require('mongodb'); var env = require('./../env')(); -var util = require('./helpers/util'); +var util = require('./util'); main(); function main() { var MongoClient = mongodb.MongoClient; - MongoClient.connect(env.storageURI, function connected(err, db) { + MongoClient.connect(env.storageURI, { "useUnifiedTopology" : true, "useNewUrlParser" : true }, function connected(err, client) { console.log('Connecting to mongo...'); if (err) { console.log('Error occurred: ', err); throw err; } + + var db = client.db(); + populate_collection(db); }); }