[JAVA-2093] Shortcut for doc lookup by _id Created: 19/Jan/16  Updated: 19/Oct/16  Resolved: 05/Oct/16

Status: Closed
Project: Java Driver
Component/s: API, Query Operations
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Blake Bauman Assignee: Jeffrey Yemin
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Very often, we need to simply look up a document by _id. What we have to do is use findOne() and updateOne() with eq("_id", <id value>). Since there's no official constant for "_id" we also have to define our own constant. This seems like something that can be easily provided in an official capacity. Ideally, this would be methods in MongoCollection like findById(<id value>), updateOneById(<id value>, <update>), and so on.



 Comments   
Comment by Githook User [ 05/Oct/16 ]

Author:

{u'username': u'jyemin', u'name': u'Jeff Yemin', u'email': u'jeff.yemin@10gen.com'}

Message: JAVA-2093: Add overloaded eq method to Filters to filter by the value of the _id field
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/27dafda0ed25724f575a2ac4e02914159eeb8364

Comment by Jeffrey Yemin [ 05/Oct/16 ]

Blake,

I'd be ok with an eqId or an overloaded eq method on Filters. I'll reopen the issue so that we can consider it further.

Comment by Blake Bauman [ 04/Oct/16 ]

This is (essentially) what we're doing now. Yes, it is a bit fragile, so we were hoping for something official. Sort of a "get document X" or "update document X" where X is an _id value. If that's not possible, what about something in Filters such as Filters.eqId(value) (sort of like Projections.excludeId() and Projections.includeId())?

Comment by Jeffrey Yemin [ 04/Oct/16 ]

Closing as Won't Fix as applications can extend MongoCollection to add specialized methods.

Comment by Jeffrey Yemin [ 04/Oct/16 ]

MongoCollection is an interface so in principle applications can extend the interface to add extra methods, e.g.:

    public interface MyMongoCollection<T> extends MongoCollection<T> {
        T findById(Object id);
    }
 
    static class MyMongoCollectionImpl<T> implements MyMongoCollection<T> {
 
        private final MongoCollection<T> wrapped;
 
        public MyMongoCollectionImpl(final MongoCollection<T> wrapped) {
            this.wrapped = wrapped;
        }
 
        public T findById(Object id) {
            return find(Filters.eq("_id", id)).first();
        }
 
        public FindIterable<T> find(Bson filter) {
            return wrapped.find(filter);
        }
        
        // implement the rest of the methods as one-liners
    }

There's some fragility here because we do occasionally add methods to MongoCollection, and that technically breaks binary compatibility for anyone implementing that interface. But as long as the user takes responsibility for re-compiling code when upgrading or downgrading the driver, it can work.

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