在我的nodejs应用程序中,我无法在单个mongoose的find方法中获取多个集合数据。使用的收藏品是“房间”和“房间用户”,他们的模型如下
房间收集模型
var mongoose = require("mongoose");
// Room Schema
var roomSchema = mongoose.Schema({
name : {
type : String
},
slug : {
type : String
},
user :{type: mongoose.Schema.Types.ObjectId, ref: 'user' },
date : {
type : Date
}
});
// This variable can be accessed from outside
var Room = module.exports = mongoose.model(
'Room',//Singular Name of database
roomSchema
);
//Data on database
[{
"_id": "5876304d23cd8e2044d5c771",
"name": "Room 1",
"user": "586f2efa79a7bb1498f5abf9",
"slug": "Room-1",
"date": "2017-01-11T13:17:01.087Z",
"__v": 0
}]
roomusers集合模型
var mongoose = require("mongoose");
// RoomUser Schema
var roomUserSchema = mongoose.Schema({
room : {type : mongoose.Schema.Types.ObjectId, ref: 'room' },
user : {type : mongoose.Schema.Types.ObjectId, ref: 'user' },
date : {type : Date}
});
// This variable can be accessed from outside
var RoomUser = module.exports = mongoose.model(
'RoomUser',//Singular Name of database
roomUserSchema
);
//Data on collection
[{
"_id": "5876304d23cd8e2044d5c772",
"room": "5876304d23cd8e2044d5c771",
"user": "586e39600e83670e1cd52246",
"date": "2017-01-11T13:17:01.186Z",
"__v": 0
}, {
"_id": "5876304d23cd8e2044d5c773",
"room": "5876304d23cd8e2044d5c771",
"user": "586e3eba36ee4722c062286b",
"date": "2017-01-11T13:17:01.190Z",
"__v": 0
}, {
"_id": "5876304d23cd8e2044d5c774",
"room": "5876304d23cd8e2044d5c771",
"user": "586f2efa79a7bb1498f5abf9",
"date": "2017-01-11T13:17:01.191Z",
"__v": 0
}, {
"_id": "5876304d23cd8e2044d5c775",
"room": "5876304d23cd8e2044d5c771",
"user": "5875f82519c5251d9c8318d1",
"date": "2017-01-11T13:17:01.193Z",
"__v": 0
}]
当我尝试获取房间数据时,它没有填充房间用户数据,我发现如下
Room.find({})
.populate('RoomUser') // I tried "roomusers" and "roomuser" still not working
.exec(callback);
#result is as follows
[{
"_id": "5876304d23cd8e2044d5c771",
"name": "Room 1",
"user": "586f2efa79a7bb1498f5abf9",
"slug": "Room-1",
"date": "2017-01-11T13:17:01.087Z",
"__v": 0
}]
我需要的是从房间查找方法获取相关的房间用户数据。我是否需要任何其他模块来填充相关表格?如果是这样的话?