【发布时间】:2020-01-22 02:53:32
【问题描述】:
这里是两个名为Patients和Hospitals的集合,我需要获取PatientId的数据,该数据在以字符串数据格式存储在patientsId下的Hospital Collections中,_id存储为ObjectId下面是详细信息:
**Hospital Collection :**
{
"_id" : ObjectId("5c04b943ff491824b806686a"),
"email" : "ayoub.khial@gmail.com",
"password" : "$2a$10$4Wt5Rn6udxREdXCIt3hGb.sKhKUKOlyiYKmLTjYG3SqEPKFSw9phq",
"PatientDetails" : {
"WeekJoined" : "Monday",
"description" : "I",
"PatientIds" : [
"5a0c6797fd3eb67969316ce2",
"5c07ada8ff49183284e509d1",
"5c07acc1ff49183284e509d0"
]
} }
**Patient Collection :**
{
"_id" : ObjectId("5a0c6797fd3eb67969316ce2"),
"picture" : "http://placehold.it/150x150",
"name" : "Genmom",
"email" : "leilaware@genmom.com",
"city" : "Rabat",
"location" : {
"type" : "Point",
"coordinates" : [
-6.79387,
33.83957
]
}
}
View Code :
{
"$lookup" : {
"from" : "Patient",
"localField" : "Hospital.PatientsDetails.PatientIds",
"foreignField" : "_id",
"as" : "PatientDetails"
}
}
如何在 MongoDB 3.6 中的 Lookup 中处理数据类型转换
【问题讨论】:
-
你试过这个
db.Hospital.aggregate([ { $lookup: { from: "patient", localField: "PatientIds", foreignField: "_id", as: "PatientDetails" } } ]) -
是的,我试过了
-
我没有看到
Patientids在PatientDetails对象内
标签: arrays mongodb mongodb-query aggregation-framework