const members = await subtable.find({ party: req.body.party }).populate({
path: "user_id", //subtable field which refers to your main table
select: "fname lname",
});
Customer.findOne({}).populate('created_by', 'name email', User)
/*Beware that when using .populate() you MUST
provide the model as mongoose will only "find"
models on the same connection. ie where:*/
var db1 = mongoose.createConnection('mongodb://localhost:27017/gh3639');
var db2 = mongoose.createConnection('mongodb://localhost:27017/gh3639_2');
var userSchema = mongoose.Schema({
"name": String,
"email": String
});
var customerSchema = mongoose.Schema({
"name" : { type: String },
"email" : [ String ],
"created_by" : { type: mongoose.Schema.Types.ObjectId, ref: 'users' },
});
var User = db1.model('users', userSchema);
var Customer = db2.model('customers', customerSchema);