这是我的架构:
/** Schemas */
var profile = Schema({
EmailAddress: String,
FirstName: String,
LastName: String,
BusinessName: String
});
var convSchema = Schema({
name: String,
users: [{
type: Schema.Types.ObjectId,
ref: 'Profiles'
}],
conversationType: {
type: String,
enum: ['single', 'group'],
default: 'single'
},
created: {
type: Date,
default: Date.now
},
lastUpdated: {
type: Date,
default: Date.now
}
});
/** Models */
db.Profiles = mongoose.model('Profiles', profile);
db.Conversations = mongoose.model('ChatConversations', convSchema);
module.exports = db;
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用以下代码(http://mongoosejs.com/docs/populate.html)填充用户:
db.Conversations.find(query).populate('users').exec(function (err, records) {
console.log(records);
});
Run Code Online (Sandbox Code Playgroud)
这是返回records …