简体   繁体   中英

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

new poster here, I found this previous post but it's on C#, I tried doing this query straight into the java code of a JSP page, for some reason, it doesn't accept the info in the {} of the find() query and just gives out an error...

So peeps, how do I do this 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 the nice little .Exclude() and .Include() commands and java doesn't? cries

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

As far as I know the official C# driver doesn't expose Include() and Exclude() methods as they violate the standard API.

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