8

I have a node app that uses 2 databases. One is the the Names and the other for the rest of all the data.

I have this connection setup:

// DATABASE CONNECTION
var APP_DB_URI = config.APP_DB; // mongodb://localhost:27017/app_db
var app_DB = mongoose.createConnection(APP_DB_URI);
var name_DB = app_DB.useDb(config.NAME_DB); // 'name_db'

This connection is working properly.

There's also no problem upon saving data to both app_db and names_db working perfect.

But the problem is this: when I try to query let say the Account model and then populate the referenced Name model, the populate process returns null.

Account schema:

 var mongoose = require('mongoose');
 var Schema = mongoose.Schema;
 var field = {
     email: {type: String, unique: true},
     password: {type: String, select: false},
     name: {type: Schema.Types.ObjectId, ref: 'Name'},
 }

 var options = {
     id: false,
     versionKey: false
 }

 var schema = new Schema(field, options);
 module.exports = mongoose.model('Account', schema);

Name schema:

 'use strict';

 var mongoose = require('mongoose'); 
 var Schema = mongoose.Schema;

 var field = {
     firstName: {type: String, required: true},
     middleName: {type: String, required: true},
     lastName: {type: String, required: true},
     suffix: {type: String, required: false,},
 }

 var options = {
     id: false,
     versionKey: false
 }

 var schema = new Schema(field, options);
 module.exports = mongoose.model('Name', schema);

The query and population:

var app_db = *app_db instance passed to this controller*    

var Account = app_db.model('Account');

Account.findOne({email:req.body.email})
    .populate('name')
    .exec(function (err, user) {
        if(user){
            // user.name returns null
        }
    })

Is there a special way to populate data from db1 the data from db2? Thanks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
jofftiquez
  • 7,548
  • 10
  • 67
  • 121

1 Answers1

15

Populate Mongo documents across databases feature added since mongoose v3.9.0. Per this across db populate test, you should different db name when model is called. Some sample codes as below. More details please refer to the test codes in the link.

var app_DB = mongoose.createConnection(APP_DB_URI);
var name_DB = app_DB.useDb(config.NAME_DB); // 'name_db'

// ...
module.exports = app_DB.model('Account', schema);

// ...
module.exports = name_DB.model('Name', schema);

Populate

.populate('name', '', Name)
jofftiquez
  • 7,548
  • 10
  • 67
  • 121
zangw
  • 43,869
  • 19
  • 177
  • 214
  • I did exactly the same. But the populate still dont work. – jofftiquez Feb 23 '16 at 08:55
  • What params exactly should I put to `.populate()` after doing this? `.populate('name')` is now working. – jofftiquez Feb 23 '16 at 09:06
  • 2
    I got it. `.populate('name', '', Name)` first is the reference key, I dont understand what the 2nd param is so I just leave it `' '` then the 3rd is the reference model – jofftiquez Feb 23 '16 at 09:16
  • 2
    @TheGreenFoxx, Refer to this [doc](http://mongoosejs.com/docs/api.html#query_Query-populate), the second param is `[select] Field selection for the population query` – zangw Feb 23 '16 at 09:17
  • 1
    @CENT1PEDE the second param defines the fields you want to be selected from the referenced model. in my case its "firstname lastname" no commas – Moses Jan 22 '20 at 07:44
  • I've spent 2 hours trying everything to get this working and your solution worked @zangw - this should be explained on the Mongoose docs that you need to specify a Model if you're using useDb because it's not explained anywhere. – Luke Brown Jun 24 '20 at 16:03