I read in the docs that Mongo can be configured to be an in memory data store. But how can I do this with Mongoose and Node? The Mongoose docs did not specify this.
Asked
Active
Viewed 1,154 times
1
-
Possible duplicate of [SO Question](https://stackoverflow.com/q/26572248/5923666) – Raz Luvaton Jul 18 '19 at 05:15
-
@RazLuvaton It is not a duplicate. I am looking to do this with Mongoose and Node. The answers to that SO question handles the configuration in the terminal via Mongo. – Mark A Jul 18 '19 at 12:58
-
1Take a look at this [npm package](https://www.npmjs.com/package/mongodb-memory-server) – Raz Luvaton Jul 18 '19 at 13:19
1 Answers
1
The mongodb-memory-server
npm package can handle setting up Mongo in memory via downloading the Mongo binaries to the node modules.
Straight from the docs:
import mongoose from 'mongoose';
import { MongoMemoryServer } from 'mongodb-memory-server';
const mongoServer = new MongoMemoryServer();
mongoose.Promise = Promise;
mongoServer.getConnectionString().then((mongoUri) => {
const mongooseOpts = {
// options for mongoose 4.11.3 and above
autoReconnect: true,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 1000,
useMongoClient: true, // remove this line if you use mongoose 5 and above
};
mongoose.connect(mongoUri, mongooseOpts);
mongoose.connection.on('error', (e) => {
if (e.message.code === 'ETIMEDOUT') {
console.log(e);
mongoose.connect(mongoUri, mongooseOpts);
}
console.log(e);
});
mongoose.connection.once('open', () => {
console.log(`MongoDB successfully connected to ${mongoUri}`);
});
});

Mark A
- 1,995
- 4
- 18
- 26