Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringData MongoDB Using projection

Is their any way via which I can use mongodb projection feature to fetch selected fields from the document.

Even after searching for whole day on internet not able to find any clue.

like image 577
tarunkumar Avatar asked Jan 30 '14 15:01

tarunkumar


Video Answer


1 Answers

Here's the relevant section of the docs concerning repositories. Also the section on querying documents has a brief mention of it.

When using MongoOperations you can use the Query instance to restrict the result to certain fields:

Query query = Query.query(Criteria.where("lastname").eq("…"));
// Configure the field spec you want to use for this query
query.fields().include(…).slice(…);

I've filed DATAMONGO-839 to improve the discoverability of this and also polish the JavaDoc of the Query class.

like image 200
Oliver Drotbohm Avatar answered Nov 14 '22 02:11

Oliver Drotbohm