MongoDB:在集合上设置TTL索引时出错:会话

时间:2014-03-27 20:36:30

标签: node.js mongodb session connect

最初此错误消息很少出现,但开始更频繁地显示,现在我运行应用程序时出现了4/5次。

我正在使用Mongo处理我的会话存储,据我所知,TTL索引用于使会话数据过期。

/home/dan/dev/audio-wave/node_modules/connect-mongo/lib/connect-mongo.js:161
            throw new Error('Error setting TTL index on collection : ' + s
                  ^
Error: Error setting TTL index on collection : sessions
at /home/dan/dev/audio-wave/node_modules/connect-mongo/lib/connect-mongo.js:161:23
at /home/dan/dev/audio-wave/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1404:28
at /home/dan/dev/audio-wave/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1542:30
at /home/dan/dev/audio-wave/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:159:22
at commandHandler (/home/dan/dev/audio-wave/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:678:48)
at Db._executeQueryCommand (/home/dan/dev/audio-wave/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1802:12)
at Cursor.nextObject (/home/dan/dev/audio-wave/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:729:13)
at Cursor.toArray (/home/dan/dev/audio-wave/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/cursor.js:158:10)
at Cursor.toArray (/home/dan/dev/audio-wave/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/scope.js:10:20)
at /home/dan/dev/audio-wave/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:1541:65

以下是将它们联系在一起的代码

var sessionStore = new MongoStore({ db: 'audio-drop' })
  , cookieParser = express.cookieParser('waytoblue')
  , SessionSockets = require('session.socket.io')
  , sockets = new SessionSockets(io, sessionStore, cookieParser);

app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.logger('dev'));
app.use(cookieParser);
app.use(express.session({
  store: sessionStore
}));

根据Mongo shell的db.version(),我正在运行2.4.9,我正在使用版本0.4.0的connect-mongo

似乎有很多人遇到了这个问题,但似乎大多数人都认为是凭据问题,我的本地mongo没有通过身份验证来保护,所以这不是问题所在。有什么想法吗?

3 个答案:

答案 0 :(得分:25)

正如我在您的评论中所说,基本上Express在会话存储完全连接之前正在接收连接。解决方案是在允许应用程序开始侦听之前等待连接发生。

您可以通过在MongoStore创建时使用回调或传入已经活动的连接来避免此问题。

使用connect-mongo回拨

的示例
var sessionStore = new MongoStore({ url: 'someConnectionUrl', db: 'audio-drop' }, function(e) {

  var cookieParser = express.cookieParser('waytoblue');
  app.use(cookieParser);

  app.use(express.session({
    store: sessionStore
  }));

  app.listen();
});

简单的Mongoose示例

var mongoose = require('mongoose');

mongoose.connect('localhost', function(e) {
  // If error connecting
  if(e) throw e;

  var sessionStore = new MongoStore({ mongoose_connection: mongoose.connection }),
      cookieParser = express.cookieParser('waytoblue');

  app.use(cookieParser);

  app.use(express.session({
    store: sessionStore
  }));

  app.listen();
});

答案 1 :(得分:6)

升级到connect-mongo版本0.8.0,对我有用。

答案 2 :(得分:0)

angular fullstack示例

  

只是将所有其他内容封装在mongoose.connect回调函数

查看我的 server / app.js

/**
 * Main application file
 */

'use strict';

// Set default node environment to development
process.env.NODE_ENV = process.env.NODE_ENV || 'development';

var express = require('express');
var mongoose = require('mongoose');
var config = require('./config/environment');

// Connect to database
mongoose.connect(config.mongo.uri, config.mongo.options , function(e){


// Populate DB with sample data
  if(config.seedDB) { require('./config/seed'); }

// Setup server
  var app = express();
  var server = require('http').createServer(app);
  var socketio = require('socket.io')(server, {
    serveClient: (config.env === 'production') ? false : true,
    path: '/socket.io-client'
  });
  require('./config/socketio')(socketio);
  require('./config/express')(app);
  require('./routes')(app);

// Start server
  server.listen(config.port, config.ip, function () {
    console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
  });

// Expose app
  exports = module.exports = app;

});

希望它有所帮助!!