[JAVA-1668] GridFS#findOne(ObjectId) finds by "objectId" instead of "_id" Created: 27/Feb/15  Updated: 27/Feb/15  Resolved: 27/Feb/15

Status: Closed
Project: Java Driver
Component/s: GridFS, Query Operations
Affects Version/s: 3.0.0
Fix Version/s: 3.0.0

Type: Bug Priority: Minor - P4
Reporter: Felix Müller [X] Assignee: Ross Lawley
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible

 Description   

GridFs#findOne(ObjectId) uses findOne(new BasicDBObject("objectId", objectId)), which does not find anything.
It should use findOne( new BasicDBObject( "_id" , objectId ) ).

That is probably just a typo.

Demo that works with 2.13.0 but not with 3.0.0-beta2:

Demo.java

public class Demo {
 
    public static void main(String[] args) throws UnknownHostException {
        MongoClient mongo = new MongoClient("localhost");
        DB db = mongo.getDB("gridDB");
        GridFS gridfs = new GridFS(db);
 
        GridFSInputFile input = gridfs.createFile(new byte[42]);
        input.setContentType("griffins");
        input.save();
 
        String id = input.getId().toString();
 
        GridFSDBFile saved = gridfs.findOne(new ObjectId(id));
        System.out.println("saved: " + saved); // should not be null
    }
}



 Comments   
Comment by Ross Lawley [ 27/Feb/15 ]

Thanks FelixM,

I happy to say this has been fixed in 7be9a11 which is currently available via the Snapshot release and will be released in the next beta.

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