Accessing "dynamic" object property in aggregation stage

Hello

I think you are asking this


Lets say you are in pipeline and you have this

  $$mydoc   = one document
  $$mykey   = one key
  
How to do 

  get("$$mydoc","$$mykey")
  contains?("$$mydoc","$$mykey")
  put("$$mydoc","$$mykey","randomValue")
  remove("$$mydoc","$$mykey")
  ....
  (and all those basic operation on maps that programming languages offer)
  

I had the same problems before sometime,in mongodb for now seems that the key can’t be in a variable.
The problem is not just the 10 lines,but object to array and back to object,is so slow to be done multiple times(for example hen reducing a array to a document).
I didn’t got any solution yet,i dont think its possible.
I managed to implement fast only put,and only if the key is new.

But if the you keep the documents with known fields (known schema),you can use unwind/group/project
etc , and if you want to do those for an embeded data,you can use facet,and do those.
Javascript is also possible with $function stage ,but its slower.

If you know where to send this either to be answered or to ask to be added on mongoDB ,would be nice.

1 Like