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.
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}`);
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.