Grab a sibling field of a matched aggregate and return a total count of all document records

192
December 29, 2017, at 07:01 AM

I'm having problems trying to construct a query that should

  • return a specified (sibling) field of a match which in this case is the changelog.histories.created field from the object that contains a Completed changelog.histories.items.toString value
  • return a complete count of all records that match the criteria (which in this case is 2 _id 5a423f48d3983274668097f3 and 5a423f48d3983274668097f3)

Below is my current aggregate query pipeline

"pipeline" => [
            [
                '$match' => [
                    'fields.project.key' => 'DW',
                    'fields.issuetype.name' => [
                        '$in' => ['Bug', 'Perceived Defect']
                    ],
                    'changelog.histories.items.field' => 'status',
                    'changelog.histories.items.toString' => 'Completed'
                ]
            ],
            [
                '$project' => [
                    'fields.issuetype.name' => 1,
                    'key' => 1,
                    'fields.summary' => 1,
                    'fields.reporter.displayName' => 1,
                    'fields.status.name' => 1,
                    'changelog.histories.created' => 1,
                ]
            ],
            [
                '$group' => [
                    "_id" => 0,
                    'count' => [
                        '$sum' => 1
                    ]
                ],
            ]
        ]
    ]

Below is a document with two example objects

[
{
   {
        "_id": {
            "$oid": "5a423f48d3983274668097f3"
        },
        "id": "59817",
        "key": "DW-15450",
        "changelog": {
            "histories": [
                {
                    "id": "449018",
                    "created": "2017-12-13T11:11:26.406+0000",
                    "items": [
                        {
                            "field": "status",
                            "toString": "Released"
                        }
                    ]
                },
                {
                    "id": "448697",
                    "created": "2017-12-08T09:54:41.822+0000",
                    "items": [
                        {
                            "field": "resolution",
                            "toString": "Fixed"
                        },
                        {
                            "field": "status",
                            "toString": "Completed"
                        }
                    ]
                }
            ]
        },
        "fields": {
            "issuetype": {
                "id": "1",
                "name": "Bug"
            }
        }
    },
    {
        "_id": {
            "$oid": "5a423f48d3983274668097f3"
        },
        "id": "59818",
        "key": "DW-15451",
        "changelog": {
            "histories": [
                {
                    "id": "449019",
                    "created": "2017-12-13T11:11:26.406+0000",
                    "items": [
                        {
                            "field": "status",
                            "toString": "Released"
                        }
                    ]
                },
                {
                    "id": "448697",
                    "created": "2017-12-08T09:54:41.822+0000",
                    "items": [
                        {
                            "field": "resolution",
                            "toString": "Fixed"
                        },
                        {
                            "field": "status",
                            "toString": "Completed"
                        }
                    ]
                }
            ]
        },
        "fields": {
            "issuetype": {
                "id": "1",
                "name": "Bug"
            }
        }
    }
]
}

Currently my query returns the following

[
{
    "result": [
        {
            "_id": 0,
            "count": 2
        }
    ],
    "ok": 1
}
]

So the ideal result of the query would look something like.

[
    {
        "result": [
            {
                "key": "DW-10518",
                "fields": {
                    "status": {
                        "name": "Completed"
                    },
                    "reporter": {
                        "displayName": "..."
                    },
                    "issuetype": {
                        "name": "Bug"
                    }
                },
                "changelog": {
                    "histories": {
                        "created": {
                            2017-12-13T11:11:26.406+0000
                        }
                    }
                }
            },
                        {
                "key": "DW-10519",
                "fields": {
                    "status": {
                        "name": "Completed"
                    },
                    "reporter": {
                        "displayName": "..."
                    },
                    "issuetype": {
                        "name": "Bug"
                    }
                },
                "changelog": {
                    "histories": {
                        "created": {
                            2017-12-13T11:11:26.406+0000
                        }
                    }
                }
            },
            {
                "_id": 0,
                "count": 2
            }
        ],
        "ok": 1
    }
]
Rent Charter Buses Company
READ ALSO
Ajax request dataType json

Ajax request dataType json

I'm having troubles displaying a value in an input fieldI did this in the past, and I haven't got a clue where my code goes wrong

263
How can I make pages within a page with AJAX? [duplicate]

How can I make pages within a page with AJAX? [duplicate]

This question already has an answer here:

138
Where do we use a multiton class for

Where do we use a multiton class for

Does someone know where you use a multiton class for (and an example)I got the question to make something in PHP using a multiton class

181