[JAVA-563] FindOne with Sort Created: 04/May/12  Updated: 29/Aug/12  Resolved: 02/Jul/12

Status: Closed
Project: Java Driver
Component/s: API
Affects Version/s: None
Fix Version/s: 2.9.0

Type: Improvement Priority: Minor - P4
Reporter: Chris Harris Assignee: Steve Briskin (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

In order to do sort on a findOne you have to:

find(query).limit(1).sort(sortBy).next()

It would be more convenient to have sort support within the findOne method, for example:

findOne(query,fields,sort)

I am using Java but this is likely to apply to many other drivers.



 Comments   
Comment by Jeffrey Yemin [ 29/Aug/12 ]

Closing as part of 2.9.0 release process.

Comment by auto [ 10/Jul/12 ]

Author:

{u'date': u'2012-06-14T09:50:46-07:00', u'name': u'Steve Briskin', u'email': u'steve.briskin@10gen.com'}

Message: JAVA-563: refactored queryopbuilder
Branch: ReadPrefs
https://github.com/mongodb/mongo-java-driver/commit/1f6c5976edcd7661d5529ecdc515dabe0e9044f2

Comment by auto [ 10/Jul/12 ]

Author:

{u'date': u'2012-06-13T12:43:27-07:00', u'name': u'Steve Briskin', u'email': u'steve.briskin@10gen.com'}

Message: JAVA-563: added orderby to findOne(). Refactored query construction to be consistent between find() and findOne().
Branch: ReadPrefs
https://github.com/mongodb/mongo-java-driver/commit/e6198e541bf64305909c1a36ceefbd57b3fe5f55

Comment by auto [ 10/Jul/12 ]

Author:

{u'date': u'2012-06-14T09:50:46-07:00', u'name': u'Steve Briskin', u'email': u'steve.briskin@10gen.com'}

Message: JAVA-563: refactored queryopbuilder
Branch: ReadPrefs
https://github.com/mongodb/mongo-java-driver/commit/3ef7a868296af7da9f277e06ce9a0ae99862e02a

Comment by auto [ 10/Jul/12 ]

Author:

{u'date': u'2012-06-13T12:43:27-07:00', u'email': u'steve.briskin@10gen.com', u'name': u'Steve Briskin'}

Message: JAVA-563: added orderby to findOne(). Refactored query construction to be consistent between find() and findOne().
Branch: ReadPrefs
https://github.com/mongodb/mongo-java-driver/commit/83fef69fc3425ccfa166e5c224b9afb277a72dec

Comment by auto [ 25/Jun/12 ]

Author:

{u'date': u'2012-06-14T09:50:46-07:00', u'name': u'Steve Briskin', u'email': u'steve.briskin@10gen.com'}

Message: JAVA-563: refactored queryopbuilder
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/1f6c5976edcd7661d5529ecdc515dabe0e9044f2

Comment by auto [ 25/Jun/12 ]

Author:

{u'date': u'2012-06-13T12:43:27-07:00', u'name': u'Steve Briskin', u'email': u'steve.briskin@10gen.com'}

Message: JAVA-563: added orderby to findOne(). Refactored query construction to be consistent between find() and findOne().
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/e6198e541bf64305909c1a36ceefbd57b3fe5f55

Comment by auto [ 25/Jun/12 ]

Author:

{u'date': u'2012-06-14T09:50:46-07:00', u'name': u'Steve Briskin', u'email': u'steve.briskin@10gen.com'}

Message: JAVA-563: refactored queryopbuilder
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/3ef7a868296af7da9f277e06ce9a0ae99862e02a

Comment by auto [ 25/Jun/12 ]

Author:

{u'date': u'2012-06-13T12:43:27-07:00', u'name': u'Steve Briskin', u'email': u'steve.briskin@10gen.com'}

Message: JAVA-563: added orderby to findOne(). Refactored query construction to be consistent between find() and findOne().
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/83fef69fc3425ccfa166e5c224b9afb277a72dec

Comment by Jeffrey Yemin [ 14/Jun/12 ]

We already have overloaded findOne with ReadPreference. This is following the logical progression of overloaded methods that we already have. It may be a mistake to have findOne at all on DBCollection, but since we do, this change increases the consistency between DBCursor and DBCollection.

Comment by Scott Hernandez (Inactive) [ 14/Jun/12 ]

We should not be expanding the API in this direction. Adding more methods on collection gets complicated and people want even more options for findOne overloads, like readPrefs, sorts, . We should make a method on cursor like oneOrNull/first() or something to special case the findOne case:

DBObject dbObj = coll.find(...).sort(...).firstOrNull();

next() is bad in this use case because it throws an exception if null/empty.

Comment by Jeffrey Yemin [ 04/May/12 ]

Yeah, pymongo has

  db.foo.find_one({'lastname': "Yemin"}, sort=[('field', 1), ('field', -1)])

Java driver could support something similar, like:

  public DBObject findOne(DBObject obj, DBObject fields, DBObject sort);

Generated at Thu Feb 08 08:52:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.