Arrays 从MongoDB 3.6中的两个集合中获取患者信息

Arrays 从MongoDB 3.6中的两个集合中获取患者信息,arrays,mongodb,mongodb-query,aggregation-framework,Arrays,Mongodb,Mongodb Query,Aggregation Framework,这是两个名为Patients和Hospitals的集合,我需要获取PatientId的数据,该数据在医院集合中以字符串数据格式存储在PatientId下,并且_id存储为ObjectId。以下是详细信息: **Hospital Collection :** { "_id" : ObjectId("5c04b943ff491824b806686a"), "email" : "ayoub.khial@gmail.com",

这是两个名为Patients和Hospitals的集合,我需要获取PatientId的数据,该数据在医院集合中以字符串数据格式存储在PatientId下,并且_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的查找中处理数据类型转换本地字段应该是PatientDetails.PatientID

如果您的
患者作为对象,即

"PatientIds" : [
  ObjectId("5a0c6797fd3eb67969316ce2"),
  ObjectId("5c07ada8ff49183284e509d1"),
  ObjectId("5c07acc1ff49183284e509d0")
];
试试这个

>>db.hospitals.aggregate([
{
$lookup:{
来自:“患者”,
localField:“PatientDetails.PatientIds”,
foreignField:“\u id”,
as:“耐心的细节”
}
}
]);
如果您的
patientid
是字符串,请尝试以下操作

>>db.hospitals.aggregate([
{
$lookup:{
来自:“患者”,
let:{patientIds:$PatientDetails.patientIds},
管道:[
{
$match:{$expr:{$in:[{$toString:$\u id“},$$patientIds”]}
}
],
as:“耐心的细节”
}
}
]);

您尝试过这个
db.Hospital.aggregate([{$lookup:{from:“patient”,localField:“PatientIds”,foreignField:“\u id”,as:“PatientDetails”}}])吗
是的,这就是我尝试过的方法。我没有看到
Patientids
PatientDetails
对象中。我尝试了第二种方法,但它不起作用,顺便说一句,我使用的是MongoDB 3.6version@uttamraoMongoDB 3.6版中没有
toString
(或
$convert
)运算符。它只在更高版本4.0中可用。在MongoDB 3.6中如何处理上述情况@prasad_顺便说一下,我可以想到几件事(但是,你可能想在谷歌上做一些研究)。(1) 将数据库更改为较新版本,或(2)转换数据(将
PatientIds
更改为
ObjectId
类型,或在
Patient
文档中使用
\u id
的字符串值创建一个新字段(可以将该字段称为
\u idStr
)@prasad_uu我们没有选择去更新版本或修改任何现有数据