简体   繁体   中英

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!

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM