Mongoose: [QUESTION] Is `getSiblingDB` available?

Created on 17 May 2018  路  4Comments  路  Source: Automattic/mongoose

Hello,

Using Mongoose v5.1.1 I could not figure out if it is possible to call the mongo method getSiblingsDB.
I've tried looking into connection.db and connection.client.db but did not find anything like it

https://docs.mongodb.com/manual/reference/method/db.getSiblingDB/

Any ideas?

docs

Most helpful comment

@JoeTheFkingFrypan getSiblingsDB() is a mongo shell helper for use, you can take advantage of the native connection's useDb() method to access another db on the same connection.

Here's a contrived example of using two databases on one connection:

6480.js

#!/usr/bin/env node
'use strict';

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/english');
const conn = mongoose.connection;
const Schema = mongoose.Schema;

const otherDB = conn.useDb('spanish');

const eSchema = new Schema({
  name: String
});

const sSchema = new Schema({
  nombre: String
});

const English = mongoose.model('test', eSchema);
const Spanish = otherDB.model('test', sSchema);

const john = new English({
  name: 'John'
});

const juan = new Spanish({
  nombre: 'Juan'
});

async function run() {
  await conn.dropDatabase();
  await otherDB.dropDatabase();
  await john.save();
  await juan.save();
  await conn.close();
  return otherDB.close();
}

run();

Shell

issues: ./6480.js
issues:
issues: mongo --quiet english
> db.tests.find()
{ "_id" : ObjectId("5afee7e237f23f65c0b8ced8"), "name" : "John", "__v" : 0 }
>
issues: mongo --quiet spanish
> db.tests.find()
{ "_id" : ObjectId("5afee7e237f23f65c0b8ced9"), "nombre" : "Juan", "__v" : 0 }
>
issues:

All 4 comments

@JoeTheFkingFrypan getSiblingsDB() is a mongo shell helper for use, you can take advantage of the native connection's useDb() method to access another db on the same connection.

Here's a contrived example of using two databases on one connection:

6480.js

#!/usr/bin/env node
'use strict';

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/english');
const conn = mongoose.connection;
const Schema = mongoose.Schema;

const otherDB = conn.useDb('spanish');

const eSchema = new Schema({
  name: String
});

const sSchema = new Schema({
  nombre: String
});

const English = mongoose.model('test', eSchema);
const Spanish = otherDB.model('test', sSchema);

const john = new English({
  name: 'John'
});

const juan = new Spanish({
  nombre: 'Juan'
});

async function run() {
  await conn.dropDatabase();
  await otherDB.dropDatabase();
  await john.save();
  await juan.save();
  await conn.close();
  return otherDB.close();
}

run();

Shell

issues: ./6480.js
issues:
issues: mongo --quiet english
> db.tests.find()
{ "_id" : ObjectId("5afee7e237f23f65c0b8ced8"), "name" : "John", "__v" : 0 }
>
issues: mongo --quiet spanish
> db.tests.find()
{ "_id" : ObjectId("5afee7e237f23f65c0b8ced9"), "nombre" : "Juan", "__v" : 0 }
>
issues:

That's exactly what I was looking for 馃槃
That you very much!


Question answered

happy to help!

Unfortunately useDb() wasn't in the connection docs, so I added it :+1:

Was this page helpful?
0 / 5 - 0 ratings