如何比较并从$ lookup中获取基于匹配字段的详细信息

时间:2017-10-10 10:55:44

标签: mongodb match aggregation-framework lookup

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

我的收藏品如下:

Users



{
	"_id" : ObjectId("59dcbbc8fcce5d16c376a2a5"),
	"organization_name" : "Blazeautomation",
	"emailId" : "uttamutterpally143@gmail.com",
	"password" : [
		"$2a$10$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",
		"$2a$10$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
                    }
                ]
            }
        ]
    }]




我想将projectId_id字段之上的字段organization_Id进行比较,并且仅使用匹配{$match:{"installer_id.assignments1.projectId":"_id"}}获得匹配结果,但我不会变空任何人都可以帮助获得欲望的结果。

1 个答案:

答案 0 :(得分:1)

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

您现在可以使用$redact

您可以在$redact

之前和之后放置{$unwind:"$installer_id.assignments1"}

$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"
    ]
  }
}