如何根据匹配字段从 $lookup 中比较和获取详细信息

how to compare and get the details from $lookup based on matching fields

我是 mongodb 的新手,我想根据我在当前响应中得到的结果从另一个 collection 获取文档,下面是我编写的用于从中获取数据的代码另一个 collection 我能够获取数据,但我需要基于条件的特定数据。

我的 Collections 如下

Users

{
 "_id" : ObjectId("59dcbbc8fcce5d16c376a2a5"),
 "organization_name" : "Blazeautomation",
 "emailId" : "uttamutterpally143@gmail.com",
 "password" : [
  "a$vwexl4DTVVXFEb8toVZNj.wETfYPqUZum2HOvltEStISkgEc4pAYy"
 ],
 "phone_number" : 9177233096,
 "organization_location" : {
  "latitude" : 32.232323,
  "longitude" : 33.2323
 },
 "isOrganization" : true,
 "isAuthenticated" : true,
 "OTP" : "194795",
 "loginNumber" : 1
}
{
 "_id" : ObjectId("59dcbc71fcce5d16c376a2a7"),
 "organization_Id" : [
  {
   "organization_Id" : ObjectId("59dcbbc8fcce5d16c376a2a5"),
   "isActive" : 0
  }
 ],
 "installer_first_name" : "raj",
 "installer_last_name" : "raj",
 "emailId" : "utterapallyuttamkumar@live.in",
 "password" : [
  "847471",
  "a$w.YeBAzcw0Lvjttgv62EyefRQ.3aKFlMVRkAHJODSMiOwpaFMATku"
 ],
 "phone_number" : "9177233096",
 "isOrganization" : false,
 "isAuthenticated" : true,
 "OTP" : "847471",
 "loginNumber" : 1,
 "isActive" : 0
}
{
 "_id" : ObjectId("59dcbdbbfcce5d16c376a2aa"),
 "organization_Id" : [
  {
   "organization_Id" : ObjectId("59dcbbc8fcce5d16c376a2a5"),
   "isActive" : 0
  }
 ],
 "installer_first_name" : "raj",
 "installer_last_name" : "raj",
 "emailId" : "uttam@live.in",
 "password" : [
  "593548"
 ],
 "phone_number" : "9177233096",
 "isOrganization" : false,
 "isAuthenticated" : false,
 "OTP" : "593548",
 "loginNumber" : 0,
 "isActive" : 0
}

Projects

{
 "_id" : ObjectId("59dcbc1ffcce5d16c376a2a6"),
 "organization_Id" : ObjectId("59dcbbc8fcce5d16c376a2a5"),
 "project_name" : "tessdfgtf12ffffd3g4dd",
 "project_description" : "Testing API's",
 "time_stamp" : "2012-11-04T14:51:06.157Z",
 "images" : {
  "path" : "ssdd/sdfsdf",
  "fileName" : "FirstImageForAPITest.png",
  "ContentType" : "png",
  "image" : "https://vlestreetlightbucket.s3.amazonaws.com/FirstImageForAPITest.png"
 },
 "installer_id" : [
  {
   "installer_id" : ObjectId("59dcbc71fcce5d16c376a2a7"),
   "installerIsActive" : 0
  },
  {
   "installer_id" : ObjectId("59dcbdbbfcce5d16c376a2aa"),
   "installerIsActive" : 0
  }
 ],
 "projectIsActive" : 0
}
{
 "_id" : ObjectId("59dcbdf2fcce5d16c376a2ac"),
 "organization_Id" : ObjectId("59dcbbc8fcce5d16c376a2a5"),
 "project_name" : "test2",
 "project_description" : "Testing API's",
 "time_stamp" : "2012-11-04T14:51:06.157Z",
 "images" : {
  "path" : "ssdd/sdfsdf",
  "fileName" : "FirstImageForAPITest.png",
  "ContentType" : "png",
  "image" : "https://vlestreetlightbucket.s3.amazonaws.com/FirstImageForAPITest.png"
 },
 "installer_id" : [
  {
   "installer_id" : ObjectId("59dcbc71fcce5d16c376a2a7"),
   "installerIsActive" : 0
  },
  {
   "installer_id" : ObjectId("59dcbdbbfcce5d16c376a2aa"),
   "installerIsActive" : 0
  }
 ],
 "projectIsActive" : 0
}

Assignments

{
 "_id" : ObjectId("59dcbcddfcce5d16c376a2a8"),
 "projectId" : ObjectId("59dcbc1ffcce5d16c376a2a6"),
 "organization_Id" : ObjectId("59dcbbc8fcce5d16c376a2a5"),
 "installer_id" : ObjectId("59dcbc71fcce5d16c376a2a7"),
 "isActive" : 0
}
{
 "_id" : ObjectId("59dcbdcbfcce5d16c376a2ab"),
 "projectId" : ObjectId("59dcbc1ffcce5d16c376a2a6"),
 "organization_Id" : ObjectId("59dcbbc8fcce5d16c376a2a5"),
 "installer_id" : ObjectId("59dcbdbbfcce5d16c376a2aa"),
 "isActive" : 0
}
{
 "_id" : ObjectId("59dcbe5cfcce5d16c376a2ad"),
 "projectId" : ObjectId("59dcbdf2fcce5d16c376a2ac"),
 "organization_Id" : ObjectId("59dcbbc8fcce5d16c376a2a5"),
 "installer_id" : ObjectId("59dcbdbbfcce5d16c376a2aa"),
 "isActive" : 0
}
{
 "_id" : ObjectId("59dcbe5cfcce5d16c376a2ae"),
 "projectId" : ObjectId("59dcbdf2fcce5d16c376a2ac"),
 "organization_Id" : ObjectId("59dcbbc8fcce5d16c376a2a5"),
 "installer_id" : ObjectId("59dcbc71fcce5d16c376a2a7"),
 "isActive" : 0
}

我获取记录的代码

 db.Projects.aggregate([   {$match:{"_id":{"$in":arrayOfIds}}},
{$unwind:"$installer_id"},
{"$lookup":{from:'Users',localField:'installer_id.installer_id',foreignField:'_id',as:'installer_id'}},
{$unwind:"$installer_id"},
{$group:{_id:"$_id","organization_Id":{$first:"$organization_Id"},"project_name":{$first:"$project_name"},"project_description":{$first:"project_description"},"time_stamp":{$first:"$time_stamp"},"project_logo":{$first:"$project_logo"},"projectIsActive":{$first:"$projectIsActive"},installer_id:{"$push":"$installer_id"}}},
                                 {$project:{"_id":1,"organization_Id":1,"project_name":1,"project_description":1,"time_stamp":1,"project_logo":1,"projectIsActive":1,"installer_id._id":1,"installer_id.installer_first_name":1,"installer_id.installer_last_name":1,"installer_id.emailId":1}},                                  
 {$unwind:"$installer_id"},
 {"$lookup":{from:'Assignments',localField:'installer_id._id',foreignField:'installer_id',as:'installer_id.assignments1'}},
 {$unwind:"$installer_id.assignments1"},
{$group:{_id:"$_id","organization_Id":{$first:"$organization_Id"},"project_name":{$first:"$project_name"},"project_description":{$first:"project_description"},"time_stamp":{$first:"$time_stamp"},"project_logo":{$first:"$project_logo"},"projectIsActive":{$first:"$projectIsActive"},installer_id:{"$push":"$installer_id"}}}]).pretty()

                                

我得到的结果是

[{
        "_id": "59d4b35ac4b6210eaa151b34",
        "organization_Id": "59d4acb2d162660d6bf14416",
        "project_name": "test123",
        "project_description": "project_description",
        "time_stamp": "2012-11-04T14:51:06.157Z",
        "project_logo": {
            "path": "ssdd/sdfsdf",
            "filename": "FirstImageForAPITest.png",
            "ContentType": "png",
            "image": "https://vlestreetlightbucket.s3.amazonaws.com/FirstImageForAPITest.png"
        },
        "projectIsActive": 0,
        "installer_id": [
            {
                "_id": "59d4accbd162660d6bf14417",
                "installer_first_name": "uttam",
                "installer_last_name": "kumar",
                "emailId": "uttam@blazeautomation.com",
                "assignments1": [
                    {
                        "_id": "59d62664e3387e0b985c260d",
                        "projectId": "59d4b35ac4b6210eaa151b34",
                        "organization_Id": "59d4acb2d162660d6bf14416",
                        "installer_id": "59d4accbd162660d6bf14417",
                        "isActive": 0
                    },
                    {
                        "_id": "59d631d1847b140c679ae4dd",
                        "projectId": "59d4ed2c35132a16073c0375",
                        "organization_Id": "59d4acb2d162660d6bf14416",
                        "installer_id": "59d4accbd162660d6bf14417",
                        "isActive": 0
                    },
                    {
                        "_id": "59db1541cb828d203a708dcf",
                        "projectId": "59db14d7cb828d203a708dce",
                        "organization_Id": "59d4acb2d162660d6bf14416",
                        "installer_id": "59d4accbd162660d6bf14417",
                        "isActive": 0
                    }
                ]
            },
            {
                "_id": "59d4accbd162660d6bf14417",
                "installer_first_name": "uttam",
                "installer_last_name": "kumar",
                "emailId": "uttam@blazeautomation.com",
                "assignments1": [
                    {
                        "_id": "59d62664e3387e0b985c260d",
                        "projectId": "59d4b35ac4b6210eaa151b34",
                        "organization_Id": "59d4acb2d162660d6bf14416",
                        "installer_id": "59d4accbd162660d6bf14417",
                        "isActive": 0
                    },
                    {
                        "_id": "59d631d1847b140c679ae4dd",
                        "projectId": "59d4ed2c35132a16073c0375",
                        "organization_Id": "59d4acb2d162660d6bf14416",
                        "installer_id": "59d4accbd162660d6bf14417",
                        "isActive": 0
                    },
                    {
                        "_id": "59db1541cb828d203a708dcf",
                        "projectId": "59db14d7cb828d203a708dce",
                        "organization_Id": "59d4acb2d162660d6bf14416",
                        "installer_id": "59d4accbd162660d6bf14417",
                        "isActive": 0
                    }
                ]
            },
            {
                "_id": "59d4accbd162660d6bf14417",
                "installer_first_name": "uttam",
                "installer_last_name": "kumar",
                "emailId": "uttam@blazeautomation.com",
                "assignments1": [
                    {
                        "_id": "59d62664e3387e0b985c260d",
                        "projectId": "59d4b35ac4b6210eaa151b34",
                        "organization_Id": "59d4acb2d162660d6bf14416",
                        "installer_id": "59d4accbd162660d6bf14417",
                        "isActive": 0
                    },
                    {
                        "_id": "59d631d1847b140c679ae4dd",
                        "projectId": "59d4ed2c35132a16073c0375",
                        "organization_Id": "59d4acb2d162660d6bf14416",
                        "installer_id": "59d4accbd162660d6bf14417",
                        "isActive": 0
                    },
                    {
                        "_id": "59db1541cb828d203a708dcf",
                        "projectId": "59db14d7cb828d203a708dce",
                        "organization_Id": "59d4acb2d162660d6bf14416",
                        "installer_id": "59d4accbd162660d6bf14417",
                        "isActive": 0
                    }
                ]
            },
            {
                "_id": "59d4accbd162660d6bf14417",
                "installer_first_name": "uttam",
                "installer_last_name": "kumar",
                "emailId": "uttam@blazeautomation.com",
                "assignments1": [
                    {
                        "_id": "59d62664e3387e0b985c260d",
                        "projectId": "59d4b35ac4b6210eaa151b34",
                        "organization_Id": "59d4acb2d162660d6bf14416",
                        "installer_id": "59d4accbd162660d6bf14417",
                        "isActive": 0
                    },
                    {
                        "_id": "59d631d1847b140c679ae4dd",
                        "projectId": "59d4ed2c35132a16073c0375",
                        "organization_Id": "59d4acb2d162660d6bf14416",
                        "installer_id": "59d4accbd162660d6bf14417",
                        "isActive": 0
                    },
                    {
                        "_id": "59db1541cb828d203a708dcf",
                        "projectId": "59db14d7cb828d203a708dce",
                        "organization_Id": "59d4acb2d162660d6bf14416",
                        "installer_id": "59d4accbd162660d6bf14417",
                        "isActive": 0
                    }
                ]
            },
            {
                "_id": "59d4accbd162660d6bf14417",
                "installer_first_name": "uttam",
                "installer_last_name": "kumar",
                "emailId": "uttam@blazeautomation.com",
                "assignments1": [
                    {
                        "_id": "59d62664e3387e0b985c260d",
                        "projectId": "59d4b35ac4b6210eaa151b34",
                        "organization_Id": "59d4acb2d162660d6bf14416",
                        "installer_id": "59d4accbd162660d6bf14417",
                        "isActive": 0
                    },
                    {
                        "_id": "59d631d1847b140c679ae4dd",
                        "projectId": "59d4ed2c35132a16073c0375",
                        "organization_Id": "59d4acb2d162660d6bf14416",
                        "installer_id": "59d4accbd162660d6bf14417",
                        "isActive": 0
                    },
                    {
                        "_id": "59db1541cb828d203a708dcf",
                        "projectId": "59db14d7cb828d203a708dce",
                        "organization_Id": "59d4acb2d162660d6bf14416",
                        "installer_id": "59d4accbd162660d6bf14417",
                        "isActive": 0
                    }
                ]
            },
            {
                "_id": "59d4accbd162660d6bf14417",
                "installer_first_name": "uttam",
                "installer_last_name": "kumar",
                "emailId": "uttam@blazeautomation.com",
                "assignments1": [
                    {
                        "_id": "59d62664e3387e0b985c260d",
                        "projectId": "59d4b35ac4b6210eaa151b34",
                        "organization_Id": "59d4acb2d162660d6bf14416",
                        "installer_id": "59d4accbd162660d6bf14417",
                        "isActive": 0
                    },
                    {
                        "_id": "59d631d1847b140c679ae4dd",
                        "projectId": "59d4ed2c35132a16073c0375",
                        "organization_Id": "59d4acb2d162660d6bf14416",
                        "installer_id": "59d4accbd162660d6bf14417",
                        "isActive": 0
                    },
                    {
                        "_id": "59db1541cb828d203a708dcf",
                        "projectId": "59db14d7cb828d203a708dce",
                        "organization_Id": "59d4acb2d162660d6bf14416",
                        "installer_id": "59d4accbd162660d6bf14417",
                        "isActive": 0
                    }
                ]
            }
        ]
    }]

我想将 projectIdorganization_Id 字段上方的字段 _id 进行比较,并且只获得匹配的结果 我尝试使用 match {$match:{"installer_id.assignments1.projectId":"_id"}} 但我没有获取空数组任何人都可以帮助获得所需的结果。

$match 无法比较文档字段。在即将推出的 3.6 版本中可以使用 $expr.

您现在可以使用 $redact

您可以在{$unwind:"$installer_id.assignments1"}

前后放置$redact

$unwind

之后
{
  "$redact": {
    "$cond": [
      {
        "$eq": [
          "$installer_id.assignments1.projectId",
          "$_id"
        ]
      },
      "$$KEEP",
      "$$PRUNE"
    ]
  }
}

之前$unwind

{
  "$redact": {
    "$cond": [
      {
        "$eq": [
          {
            "$ifNull": [
              "$projectId",
              "$$ROOT._id"
            ]
          },
          "$$ROOT._id"
        ]
      },
      "$$DESCEND",
      "$$PRUNE"
    ]
  }
}

使用 3.6 $expr

{
  "$match": {
    "$expr": [
      "$installer_id.assignments1.projectId",
      "$_id"
    ]
  }
}