Храню сессии в монго. Появилсь необходимость сделать поиск по всех открытих сессиях. Ток не получається сделать по-простому
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
app.use(session({
secret : 'poemsSessionToken_BvaPz14a2Ob0pUrNIj4Uddf',
resave : false,
saveUninitialized: false,
cookie : {
maxAge: 2 * 365 * 24 * 60 * 60 * 1000 // two years
},
store : new MongoStore({
host : process.env.DB_HOST,
port : process.env.DB_PORT,
db : process.env.DB_NAME,
autoReconnect: true,
ssl : false
})
}));
var collection = db.collection('sessions');
collection.find({}, function(err, result){
console.log(result)
})
В ответ получаю беду
{ connection: null,
server: null,
disconnectHandler:
{ s: { storedOps: [], storeOptions: [Object], topology: [Object] },
length: [Getter] },
bson: {},
ns: 'PoemsDev.sessions',
cmd:
{ find: 'PoemsDev.sessions',
limit: 0,
skip: 0,
query: {},
slaveOk: false },
options:
{ skip: 0,
limit: 0,
raw: undefined,
hint: null,
timeout: undefined,
slaveOk: false,
db:
{ domain: null,
_events: [Object],
_maxListeners: undefined,
s: [Object],
serverConfig: [Getter],
bufferMaxEntries: [Getter],
databaseName: [Getter],
options: [Getter],
native_parser: [Getter],
slaveOk: [Getter],
writeConcern: [Getter] },
promiseLibrary: [Function: Promise],
disconnectHandler: { s: [Object], length: [Getter] } },
topology:
{ domain: null,
_events:
{ reconnect: [Function],
timeout: [Object],
error: [Object],
close: [Function],
destroy: [Object] },
_maxListeners: undefined,
s:
{ options: [Object],
callbacks: [Object],
logger: [Object],
state: 'connected',
reconnect: true,
reconnectTries: 30,
reconnectInterval: 1000,
emitError: true,
currentReconnectRetry: 30,
ismaster: [Object],
readPreferenceStrategies: undefined,
authProviders: [Object],
id: 0,
tag: undefined,
disconnectHandler: [Object],
wireProtocolHandler: [Object],
Cursor: [Object],
bsonInstance: {},
bson: {},
pool: [Object],
serverDetails: [Object] },
name: [Getter],
bson: [Getter],
wireProtocolHandler: [Getter],
id: [Getter] },
cursorState:
{ cursorId: null,
cmd:
{ find: 'PoemsDev.sessions',
limit: 0,
skip: 0,
query: {},
slaveOk: false },
documents: [],
cursorIndex: 0,
dead: false,
killed: false,
init: false,
notified: false,
limit: 0,
skip: 0,
batchSize: 1000,
currentLimit: 0,
transforms: undefined },
callbacks: null,
logger: { className: 'Cursor' },
_readableState:
{ objectMode: true,
highWaterMark: 16,
buffer: [],
length: 0,
pipes: null,
pipesCount: 0,
flowing: null,
ended: false,
endEmitted: false,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events: {},
_maxListeners: undefined,
s:
{ numberOfRetries: 5,
tailableRetryInterval: 500,
currentNumberOfRetries: 5,
state: 0,
streamOptions: {},
bson: {},
ns: 'PoemsDev.sessions',
cmd:
{ find: 'PoemsDev.sessions',
limit: 0,
skip: 0,
query: {},
slaveOk: false },
options:
{ skip: 0,
limit: 0,
raw: undefined,
hint: null,
timeout: undefined,
slaveOk: false,
db: [Object],
promiseLibrary: [Function: Promise],
disconnectHandler: [Object] },
topology:
{ domain: null,
_events: [Object],
_maxListeners: undefined,
s: [Object],
name: [Getter],
bson: [Getter],
wireProtocolHandler: [Getter],
id: [Getter] },
topologyOptions:
{ socketOptions: {},
auto_reconnect: true,
host: 'localhost',
port: 27017,
cursorFactory: [Object],
reconnect: true,
emitError: true,
size: 5,
disconnectHandler: [Object],
bson: {},
messageHandler: [Function],
wireProtocolHandler: [Object] },
promiseLibrary: [Function: Promise],
currentDoc: null },
timeout: false,
sortValue: undefined,
readPreference: undefined }
на оверфлоу подсмотрел такую штуку, которая возвращает нужные модели на странно выглядит
collection.find().toArray(function (err, sessions) {
console.log(sessions);
});
//в консоль
[ { _id: 'rgVJZO4BCEPsZuRNjZIfWE0y8bYFn4jM',
session: '{"cookie":{"originalMaxAge":63072000000,"expires":"2018-04-25T12:51:19.489Z","httpOnly":true,"path":"/"},"loggedIn":true,"userId":"570f90bf983abb78053b7e32","email":"new","role":"USER","userStatus":"ACTIVE"}',
expires: Wed Apr 25 2018 17:36:05 GMT+0300 (Финляндия (лето)) } ]
Почему и как оно работает?)
Как достать все сессии не используя такие костыли? Спасибо