EK.
EK.

Reputation: 2996

Find in mongodb subdocument

I use SpringData. I have document with subdocument, it looks like :

{ "name" : "MongoDB", "type" : "database", "count" : 1, "info" : { x : 203, y : 102 } }

How can I find all documents with(for example) x=203 Thanks!

Upvotes: 1

Views: 2420

Answers (1)

Anoop Isaac
Anoop Isaac

Reputation: 942

There is no way you can get subdocument directly. What you can do is use below query to match the value inside you subdocument. This would retrieve parent document if you criteria for the subdocument succeeds.As Rohit mentioned you can use below query but this return the type of your parent document

mongoTemplate.find(new Query(Criteria.where("info.x").is(203))), ParentDocument.class));

Upvotes: 4

Related Questions