There is a structure:

{ "A" : { "B1" : { "C" : { "K1" : { "x" : NumberLong(12), "y" : NumberLong(13) } } }, "B2" : { "C" : { "K1" : { "x" : NumberLong(35), "y" : NumberLong(45) }, "K2" : { "x" : NumberLong(1), "y" : NumberLong(0) } } } } } 

There is a task: Select all K1 (the variable that is passed) from all B n

I tried it myself but there are problems:

  1. How to specify not explicitly B1 , but all B n
  2. As in the result of category to push the very name of the key B n
 .aggregate([ {$match:{ "A.B1.C.K1":{ $exists:true } }}, {$project:{ "result":"A.B1.C.K1", "category":"B1", "_id":false }} ]); 
  • And from the collection by what criterion should the documents be selected? - Vladimir Gamalyan
  • Select all K1 from A - Ninazu
  • I mean from the mongodb collection. This is your one document. By what criterion is he selected or is he the only one in the collection? - Vladimir Gamalyan
  • If the selection of documents is not required, the task can be easily solved on the client. Because In Monge, as far as I know, there is no wild support for the paths within the document (and in fact it cannot be, most likely, because for example an array can be located along the path), and in the aggregation framework there are no dynamic names (for now). - Vladimir Gamalyan
  • It’s a pity (((I greatly simplified the example. Actually, I have a huge tree with a bunch of branches and nodes. And I just need to find all the occurrences of the child node plus pull out part of the way. It’s expensive to sort through the resources, I thought it can be a single request pull out just what you need - Ninazu

0