节点查找不返回聚合数据
Node Lookup not returning aggregated data
我的 Node 应用程序中有 2 个约会和客户集合,我想显示约会以及与约会相关的用户。我已经设置如下但我没有得到用户作为输出。
var userSchema = new mongoose.Schema({
firstname: String,
surname: String,
email: String,
});
var appointmentSchema = new mongoose.Schema({
userID: String,
type: Number,
time: Date
});
我的查找查询是这样的:-
Appointment.aggregate([
{
$match: {"type": 1}
},
{
$lookup:{
from: "users",
localField: "userID",
foreignField: "_id",
as: "appointmentUser"
}
}
], function(err,foundAppointmentUser) {
if(err){
console.log(err);
} else {
console.log(foundAppointmentUser);
}
});
mongo中的数据来了,预约数据:-
{
"_id": {
"$oid": "5bcc8eac7ac5980bfa365183"
},
"userID": "5bb4d1945480e60771ccde5a",
"type": 1,
"time": {
"$date": "2018-10-22T10:00:00.000Z"
},
"__v": 0
}
用户数据:
{
"_id": {
"$oid": "5bb4d1945480e60771ccde5a"
},
"firstname": "Stu",
"surname": "Test 999",
"email": "stu@stu.com",
"created": {
"$date": "2018-10-03T14:26:28.815Z"
},
"__v": 0
}
这是结果:-
[ { _id: 5bcc8eac7ac5980bfa365183,
userID: '5bb4d1945480e60771ccde5a',
type: 1,
time: 2018-10-22T10:00:00.000Z,
__v: 0,
appointmentUser: [] } ]
这是检索此类数据的最佳方式吗?另外,为什么我没有收到 appointmentUser
数组下的用户数据?提前致谢。
如下更改架构:仅供参考,Populate
var appointmentSchema = new mongoose.Schema({
userID: {
type: Schema.Types.ObjectId,
ref: 'user'
},
type: Number,
time: Date
});
参考 ID (UserId)
的类型应为 ObjectId
而不是 String
。
我的 Node 应用程序中有 2 个约会和客户集合,我想显示约会以及与约会相关的用户。我已经设置如下但我没有得到用户作为输出。
var userSchema = new mongoose.Schema({
firstname: String,
surname: String,
email: String,
});
var appointmentSchema = new mongoose.Schema({
userID: String,
type: Number,
time: Date
});
我的查找查询是这样的:-
Appointment.aggregate([
{
$match: {"type": 1}
},
{
$lookup:{
from: "users",
localField: "userID",
foreignField: "_id",
as: "appointmentUser"
}
}
], function(err,foundAppointmentUser) {
if(err){
console.log(err);
} else {
console.log(foundAppointmentUser);
}
});
mongo中的数据来了,预约数据:-
{
"_id": {
"$oid": "5bcc8eac7ac5980bfa365183"
},
"userID": "5bb4d1945480e60771ccde5a",
"type": 1,
"time": {
"$date": "2018-10-22T10:00:00.000Z"
},
"__v": 0
}
用户数据:
{
"_id": {
"$oid": "5bb4d1945480e60771ccde5a"
},
"firstname": "Stu",
"surname": "Test 999",
"email": "stu@stu.com",
"created": {
"$date": "2018-10-03T14:26:28.815Z"
},
"__v": 0
}
这是结果:-
[ { _id: 5bcc8eac7ac5980bfa365183,
userID: '5bb4d1945480e60771ccde5a',
type: 1,
time: 2018-10-22T10:00:00.000Z,
__v: 0,
appointmentUser: [] } ]
这是检索此类数据的最佳方式吗?另外,为什么我没有收到 appointmentUser
数组下的用户数据?提前致谢。
如下更改架构:仅供参考,Populate
var appointmentSchema = new mongoose.Schema({
userID: {
type: Schema.Types.ObjectId,
ref: 'user'
},
type: Number,
time: Date
});
参考 ID (UserId)
的类型应为 ObjectId
而不是 String
。