How to get a subset of fields using the Java MongoDB driver?

the new poster is here, I found this previous post, but it is in C #, I tried to execute this request directly in the java code of the JSP page, for some reason it does not accept information in {} of the find () query and just throws an error. ..

So how to do it in Java:

// retrieve ssn field for documents where last_name == 'Smith': db.users.find({last_name: 'Smith'}, {'ssn': 1}); 

Thanks!

PS: why the hell does C # have good .Exclude () and .Include () commands, but java not? screams

+4
source share
1 answer

The java driver follows the same API as the shell. Just pass the DBObject containing your field projection as the second argument to find or findOne

As far as I know, the official C # driver does not provide Include () and Exclude () methods, since they violate the standard API.

+2
source

All Articles