Use aggregation logic for conditional operation on array in bridge key

I have a collection that contains “bridge keys” with the id of a doc in another collection and an array of the ids of docs connected to the first doc in another collection like this:

collection: centralReports
doc = centralReport: {
     _id: centralReportId,

collection: bridgeKeys
doc = bridgeKey: {
      _id: bridgeKeyId,
      centralReportId: centralReportId , 
      connectedReportIds: [ reportId, anotherReportId, yetAnotherReportId ] 

collection: reports
doc = report: {
   _id: reportId

I want to search the collection of bridge keys by centralReportId, and if there is no bridgeKey that has that centralReportId, I want to create one and create a new report and add the reportId to the array connectedReportIds in the bridgeKey doc.

But if there is already a doc that contains a particular centralReportId, if there are any ids in the array connectedReportIds, I want to return that reportId, but if there isn’t, I want to create a new doc in the collection reports and add the _id of that reportId to the array connectedReportIds.

So far with a list of different mongodb function calls I do:

  • if no bridge key exists with this session id, create one
  • find the bridge key with this centralReportId (because upsert on the prev call means no object is returned unless it creates one)
  • create new report object
  • if no reportIds in bridgeKey, then create a new report with a new id and add that id to array of reportIds in bridgeKey
  • find new bridgeKey by centralReportId and return it

This seems massively inefficient and prone to failure

Is there a simpler way to do this with an aggregation?