Quantcast
Channel: Spring Community Forums - NoSQL
Viewing all articles
Browse latest Browse all 128

MongoDB Restrict Fields Returned in Search (Repository & Template)

$
0
0
I use MongoDB and Spring Data. I typically use repositories to perform CRUD operations. I would like to be able to query a collection, but limit the fields that are returned.

If I have:

Code:

public class MyClass {
    String _id;
    String lastName;
    String firstName;
    ... many more fields ...
};

I know I can do:

Code:

db.mycollection.find({lastName: "Jones"},{lastName:1, firstName:1, socialsecurity:0})
Something like this would return lastName and firstName.

How can I duplicate something like this using Spring data.

Can this be done in a repository by annotating a query to exclude certain fields (or include certain fields)? So I would like to use a single domain object, but only populate the fields that are of interest.

Any thoughts?

Thanks,
Tom

Viewing all articles
Browse latest Browse all 128

Trending Articles