I'm working with Node.js + Express + Mongoose, and have a Mongoose Schema that I use to store "session" data. At the same time I'm using 'connect-mongodb' for session management (native driver).
Only today I've realized that I have redundant functionality.
So, I've tried to add my Schema object to 'req.session'. It seems to work, but I loose the Moongose functionality for my Schema objects in next requests.
One option I consider is to store in the 'req.session', the id of the object and then use Mongoose to retrieve it.
Another option I consider is to replace 'connect-mongoose' with a Mongoose session manager.
Can you please help me with the decision, and recommend a way. And more specific, if I use a Mongoose solution, will it solve the issue with loosing the Mongoose functionality with my Schema objects?
TIA
My advice is to keep Mongoose completely out of your session management and only persist plain JavaScript objects in your session on req.session
. That way you can use connect-mongodb as you are now or switch to some other session store like connect-redis very easily if you want to isolate your databases. If you need to frequently access Mongoose objects that feel like part of the session, persist those objects' objectIds as strings in the session so they can be quickly and easily looked up as needed.