Reputation: 91
I have multiple mongodb databases, each database has the same collections and the data has the same structure. I will use mongoose to access / write this data but for this i need to select the database at each opperation.
I tried this, but it's not working (it's still using the DB from the connection string):
mongoose.connection.useDb('myDB')
const data = new DataSchema({test: "Hello"})
data.save()
Upvotes: 0
Views: 1078
Reputation: 659
try this:
const db = mongoose.connection.useDb('myDB')
const data = db.model("Data", DataSchema({test: "Hello"}))
data.save()
Following has worked for me in the past
const db = mongoose.connection.useDb('bucket_db');
const BucketCollection = db.model("Buckets", BucketSchema);
const bucket = await BucketCollection.findOne({ OldId: customer.BucketId });
Basically, you have to tell mongoose to use the Schema with new db you have just connected to
Upvotes: 1