findOne () Java

126 views
Skip to first unread message

Ben Hill

unread,
Jan 19, 2016, 4:03:48 PM1/19/16
to mongodb-user
Hi,

I cannot find the findOne method listed i only get findoneandupdate listed as an option in my IDE. Which in Intelijj. have have updated the maven repo etc and still no joy. Any ideas?

HubertBubert

unread,
Jan 19, 2016, 5:59:48 PM1/19/16
to mongodb-user
Hello,

I bielieve it's simply becouse there is no such method and probably never was.
So you should use find() with first().

Example (java driver):

user = usersCollection.find(eq("_id", username)).first();

cheers,
Hubert

Wan Bachtiar

unread,
Jan 19, 2016, 11:05:13 PM1/19/16
to mongodb-user

Hi Ben,

Just to clarify, findOne() method does exist in the current MongoDB Java Driver (v3.2).

Although this is no longer exposed in the MongoCollection public interface. Which is probably what IntelliJ IDEA is using for the auto-complete.

As mentioned in the mongo-java-driver QuickTour, you can use first() instead. For example:

Document myDoc = collection.find().first();
System.out.println(myDoc.toJson());

Regards,
Wan.

Reply all
Reply to author
Forward
0 new messages