尝试使用$lookup“join”操作时,结果计数正常,但“as”文档为空
我有两个集合,我需要从SubscriberCol获取用户详细信息,用户ID在Employer\Jobscol中
下标

{
    "_id" : ObjectId("58187e7551d244640626d7e1"),
    "type" : "job_seeker",
    "firstname" : "vishnu",
    "lastname" : "kumar pv",
    "email_array" : {
        "primary" : "[email protected]",
        "secondary" : "[email protected]",
        "verified" : false
    },
    "address_array" : {
        "address" : "test address22d",
        "streetname" : "test222d",
        "pincode" : "test222d",
        "city" : "dddd",
        "state" : "ALASKA2d",
        "country" : "Argentinad"
    },
    "phone_array" : {
        "primary" : "",
        "secondary" : "",
        "verified" : ""
    },
    "languages" : [
        "english",
        "malayalam",
        "english2"
    ]
}

雇主
{
    "_id" : ObjectId("582ada6b51d244073e2a7541"),
    "employer_id" : ObjectId("58187e7551d244640626d7e1"),
    "job_id" : "testjob16946",
    "job_title" : "Test Job 25",
    "category" : "IT",
    "vacancies" : "5",
    "salary" : "200000",
    "location" : "Kollam",
    "employer_name" : "test test",
    "mobile" : "9123456987",
    "video" : "",
    "image" : "",
    "work_place" : "option1",
    "email" : "[email protected]",
    "skills" : [
        "php"
    ],
    "isActive" : true,
    "applied_users" : [
        {
            "user_id" : ObjectId("581b364751d2445c311cf6f1"),
            "accepted" : false
        },
        {
            "user_id" : ObjectId("58187e7551d244640626d7e1"),
            "accepted" : false
        }
    ]
}

我在这里的数据库查询(用robomongo执行)
db.getCollection('employer_jobscol').aggregate([   {
      $unwind: "$applied_users"
   },
    {
      $lookup:
        {
          from: "subscribercol",
          localField: "user_id",
          foreignField: "_id",
          as: "subscribercol_docs"
        }
   }
])

结果是
{
    "_id" : ObjectId("582ada6b51d244073e2a7541"),
    "employer_id" : ObjectId("58187e7551d244640626d7e1"),
    "job_id" : "testjob16946",
    "job_title" : "Test Job 25",
    "category" : "IT",
    "vacancies" : "5",
    "salary" : "200000",
    "location" : "Kollam",
    "employer_name" : "test test",
    "mobile" : "9123456987",
    "video" : "",
    "image" : "",
    "work_place" : "option1",
    "email" : "[email protected]",
    "skills" : [
        "php"
    ],
    "isActive" : true,
    "applied_users" : {
        "user_id" : ObjectId("58187e7551d244640626d7e1"),
        "accepted" : false
    },
    "subscribercol_docs" : []
}

这里subscribercol_docs是一个空数组,我需要用户信息(姓名、地址等)。
php - Mongodb聚合$ lookup返回空数组-LMLPHP

最佳答案

因为本地文档中没有用户id字段,所以它的“applied\u users.user\u id”
试试这个

db.getCollection('employer_jobscol').aggregate([   {
      $unwind: "$applied_users"
   },
    {
      $lookup:
        {
          from: "subscribercol",
          localField: "applied_users.user_id", // <-- check here
          foreignField: "_id",
          as: "subscribercol_docs"
        }
   }
])

关于php - Mongodb聚合$ lookup返回空数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40647549/

10-10 00:36