1

I have a node js application with many files. Three of them have the following declarations respectively:

mongoose2.connect("mongodb://localhost:27017/terms");
mongoose.connect("mongodb://localhost:27017/results");
mongoose.connect("mongodb://localhost:27017/users");

The problem is that this for some reason does not work. The data are saved in database, but mongoose is confused and each time they are saved randomly among terms, results and users. Do you know why this is happening and a workaround maybe ?

2

1 Answer 1

1

Instead of creating 3 separate mongo connections it's better to switch between dbs using useDb() method.

const mongoose = require("mongoose");
const termsConn = mongoose.createConnection("mongodb://localhost:27017/terms");
//... code

const resultsConn = temmsConn.useDb("results");
console.log(resultsConn.name); // => results
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.