"File" : "58286ce0d43ee415567ab4e5",
"Folder" : "5829d284d43ee41fa74f7e50"
},
"UserID" : "1"
}
{
"_id" : ObjectId("5829d284d43ee41fa74f7e50"),
"Children" : {
"File" : "58286ce0d43ee415567ab4e6",
"Folder" : "5829d284d43ee41fa74f7e51"
},
"UserID" : "1"
}
So now define a view on "folders":
db.createView("foldersKeynamesFixed", "folders", [ {$addFields:{Children:{$arrayToObject:{$map:{input:{$objectToArray:"$Children"}, in:{k:{$substr:["$$this.k",0,{$indexOfCP:["$$this.k","_"]}]},v:"$$this.v"}}}}}} ])
And now the keys in "Children" are "fixed" - no extra string there.
Can you now do $graphLookup?
You can but you won't get the right output, and the reason is that you can't compare ObjectId() type with string type and get equality, and currently the aggregation pipeline does not have a method to convert between ObjectId and string (adding this improvement is tracked in SERVER-24947
Here's how it would work if you were storing ObjectId values rather than their string equivalents:
> db.folders.find().pretty()
{
"_id" : ObjectId("5829d284d43ee41fa74f7e50"),
"Children" : {
"File_58286ce0d43ee415567ab4e6" : ObjectId("58286ce0d43ee415567ab4e6"),
"Folder_5829d284d43ee41fa74f7e51" : ObjectId("5829d284d43ee41fa74f7e51")
},
"UserID" : "1"
}
{