0

I have collection like this example

 { "_id" : { "$oid" : "594989c638773060134b8442" },
"rank" : 10.0, "brand" : "Dodge", "model" : "Viper",
"engineSpec" : { "cylinderConfig" : "V-10", "engineLiters" : 8.4, "transmission" : "6 speed manual" } }

if i make request

 collection.find()
.projection(Projections.include("engineSpec"))
.forEach((Block<Document>) set ->{
                System.out.println(set.toJson());
            });

i get like this

{ "_id" : { "$oid" : "594989c638773060134b8442" },
"engineSpec" : { "cylinderConfig" :
"V-10",
"engineLiters" : 8.4,
"transmission" : "6 speed manual" } }

I want to get the set as in this example

{ "_id" : { "$oid" : "594989c638773060134b8442" },
"engineSpec" : {"transmission" : "6 speed manual" } }
Ihor Fedchenko
  • 81
  • 1
  • 1
  • 3
  • `Projections.include("engineSpec.transmission")` – Neil Lunn Jun 24 '17 at 22:07
  • Thanks, but if i want get only "transmission" : "value" without "engineSpec", how to do it? – Ihor Fedchenko Jun 25 '17 at 04:58
  • You don't really. That is your document structure, and we did omit the unwanted fields, so you really should learn to live with this. The only way we can "structurally alter" documents beyond "removal of keys" is using the aggregation framework. However, it does involve an overhead for very little gain. So in best practice, you should simply accept that you did at least "reduce some data" to only what was necessary, and simply access the element by it's known path. In short, you can do it, but it's just a waste of time to do so. – Neil Lunn Jun 25 '17 at 05:17

0 Answers0