无法在mongodb

时间:2017-10-16 05:55:14

标签: mongodb

我是mongodb的新手我正在尝试将以下记录分组是我正在使用的代码

db.Users.aggregate([{$match:{"_id":new ObjectID(details._id)}},
                                {$unwind:"$organization_Id"},
                                {"$lookup":{from:"Users",localField:'organization_Id.organization_Id',foreignField:'_id',as:'organization_Id'}},
                                {$unwind:"$organization_Id"},
                                {$group:{_id:"$_id",organization_Id:{"$push":"$organization_Id"}}},
                                {$project:{"organization_Id.organization_name":1,"organization_Id._id":1,"organization_Id.organization_name":1,"organization_Id.emailId":1,"organization_Id.phone_number":1}},
                                {$unwind:"$organization_Id"},
                                {"$lookup":{from:Globals.ProjectCollectionName,localField:'organization_Id._id',foreignField:'organization_Id',as:'organization_Id.projects'}},
                                {$unwind:"$organization_Id"},   
                                {$group:{_id:"$_id",organization_Id:{"$push":"$organization_Id"}}},
                               
                               {$unwind:"$organization_Id"}, 
                                {$unwind:"$organization_Id.projects"},  
                                {$unwind:"$organization_Id.projects.installer_id"}, 
                                
                                
                                {$match:{"organization_Id.projects.installer_id.installer_id":new ObjectID(details._id),"organization_Id.projects.installer_id.installerIsActive":0}},
                                 {$project:{"organization_Id.organization_name":1,"organization_Id._id":1,"organization_Id.organization_name":1,"organization_Id.emailId":1,"organization_Id.phone_number":1,"organization_Id.projects":1}},
                                {$unwind:"$organization_Id"}, 
                                {$unwind:"$organization_Id.projects"},  
                                {$unwind:"$organization_Id.projects.installer_id"},  
                                 {$group:{_id:"$_id",organization_Id:{"$push":"$organization_Id"}}}]).pretty()

我得到的回应是

Response

{
    "status": 0,
    "message": "Details.",
    "result": [
        {
            "_id": "59dcbc71fcce5d16c376a2a7",
            "organization_Id": [
                {
                    "_id": "59dcbbc8fcce5d16c376a2a5",
                    "organization_name": "Blazeautomation",
                    "emailId": "uttamutterpally143@gmail.com",
                    "phone_number": 9177233096,
                    "projects": {
                        "_id": "59dcbc1ffcce5d16c376a2a6",
                        "organization_Id": "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": "59dcbc71fcce5d16c376a2a7",
                            "installerIsActive": 0
                        },
                        "projectIsActive": 0
                    }
                },
                {
                    "_id": "59dcbbc8fcce5d16c376a2a5",
                    "organization_name": "Blazeautomation",
                    "emailId": "uttamutterpally143@gmail.com",
                    "phone_number": 9177233096,
                    "projects": {
                        "_id": "59dcbdf2fcce5d16c376a2ac",
                        "organization_Id": "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": "59dcbc71fcce5d16c376a2a7",
                            "installerIsActive": 0
                        },
                        "projectIsActive": 0
                    }
                },
                {
                    "_id": "59dcbbc8fcce5d16c376a2a5",
                    "organization_name": "Blazeautomation",
                    "emailId": "uttamutterpally143@gmail.com",
                    "phone_number": 9177233096,
                    "projects": {
                        "_id": "59dcd9cdae7ac71ffeb1dbf1",
                        "organization_Id": "59dcbbc8fcce5d16c376a2a5",
                        "project_name": "test3",
                        "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": "59dcbc71fcce5d16c376a2a7",
                            "installerIsActive": 0
                        },
                        "projectIsActive": 0
                    }
                },
                {
                    "_id": "59e08ea4d50d05140c4dd88f",
                    "organization_name": "Blazeautomation",
                    "emailId": "uttamkumar1ddd3@gmail.com",
                    "phone_number": 9177233096,
                    "projects": {
                        "_id": "59e08eb9d50d05140c4dd890",
                        "organization_Id": "59e08ea4d50d05140c4dd88f",
                        "project_name": "test4we",
                        "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": "59dcbc71fcce5d16c376a2a7",
                            "installerIsActive": 0
                        },
                        "projectIsActive": 0
                    }
                },
                {
                    "_id": "59e08ea4d50d05140c4dd88f",
                    "organization_name": "Blazeautomation",
                    "emailId": "uttamkumar1ddd3@gmail.com",
                    "phone_number": 9177233096,
                    "projects": {
                        "_id": "59e08eb9d50d05140c4dd890",
                        "organization_Id": "59e08ea4d50d05140c4dd88f",
                        "project_name": "test4we",
                        "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": "59dcbc71fcce5d16c376a2a7",
                            "installerIsActive": 0
                        },
                        "projectIsActive": 0
                    }
                },
                {
                    "_id": "59e08ea4d50d05140c4dd88f",
                    "organization_name": "Blazeautomation",
                    "emailId": "uttamkumar1ddd3@gmail.com",
                    "phone_number": 9177233096,
                    "projects": {
                        "_id": "59e08eb9d50d05140c4dd890",
                        "organization_Id": "59e08ea4d50d05140c4dd88f",
                        "project_name": "test4we",
                        "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": "59dcbc71fcce5d16c376a2a7",
                            "installerIsActive": 0
                        },
                        "projectIsActive": 0
                    }
                }
            ]
        }
    ]
}

Desire result

{
    "status": 0,
    "message": "Details.",
    "result": [
        {
            "_id": "59dcbc71fcce5d16c376a2a7",
            "organization_Id": [
                {
                    "_id": "59dcbbc8fcce5d16c376a2a5",
                    "organization_name": "Blazeautomation",
                    "emailId": "uttamutterpally143@gmail.com",
                    "phone_number": 9177233096,
                    "projects": [
                        {
                            "_id": "59dcbc1ffcce5d16c376a2a6",
                            "organization_Id": "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": "59dcbc71fcce5d16c376a2a7",
                                    "installerIsActive": 0
                                }
                            ],
                            "projectIsActive": 0
                        },
                        {
                            "_id": "59dcbdf2fcce5d16c376a2ac",
                            "organization_Id": "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": "59dcbc71fcce5d16c376a2a7",
                                    "installerIsActive": 0
                                }
                                
                            ],
                            "projectIsActive": 0
                        },
                        {
                            "_id": "59dcd9cdae7ac71ffeb1dbf1",
                            "organization_Id": "59dcbbc8fcce5d16c376a2a5",
                            "project_name": "test3",
                            "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": "59dcbc71fcce5d16c376a2a7",
                                    "installerIsActive": 0
                                }
                            ],
                            "projectIsActive": 0
                        }
                        
                    ]
                },
                {
                    "_id": "59e08ea4d50d05140c4dd88f",
                    "organization_name": "Blazeautomation",
                    "emailId": "uttamkumar1ddd3@gmail.com",
                    "phone_number": 9177233096,
                    "projects": [
                        {
                            "_id": "59e08eb9d50d05140c4dd890",
                            "organization_Id": "59e08ea4d50d05140c4dd88f",
                            "project_name": "test4we",
                            "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": "59dcbc71fcce5d16c376a2a7",
                                    "installerIsActive": 0
                                },
                                {
                                    "installer_id": "59dcbc71fcce5d16c376a2a7",
                                    "installerIsActive": 0
                                },
                                {
                                    "installer_id": "59dcbc71fcce5d16c376a2a7",
                                    "installerIsActive": 0
                                }
                            ],
                            "projectIsActive": 0
                        }
                    ]
                }
            ]
        }
    ]
}

我无法使用上面的代码形成数据我已经使用$展开数组对象然后我匹配的情况然后我尝试将它分组但我无法做到这一点我得到了错误的结果,有人可以帮我解决这个问题吗

0 个答案:

没有答案