Mongoose populate across 2 databases
Asked Answered
S

1

9

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.

Stereography answered 23/2, 2016 at 7:42 Comment(1)
@marc_s Thanks for the revisionStereography
P
16

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)
Papaveraceous answered 23/2, 2016 at 8:2 Comment(7)
I did exactly the same. But the populate still dont work.Stereography
What params exactly should I put to .populate() after doing this? .populate('name') is now working.Stereography
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 modelStereography
@TheGreenFoxx, Refer to this doc, the second param is [select] <Object, String> Field selection for the population queryPapaveraceous
@CENT1PEDE the second param defines the fields you want to be selected from the referenced model. in my case its "firstname lastname" no commasZinnia
I've spent 2 hours trying everything to get this working and your solution worked @Papaveraceous - 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.Chopin
Is there a way to set the database on the field to use for populating?Erigena

© 2022 - 2024 — McMap. All rights reserved.