enagra
enagra

Reputation: 2325

To construct GridFS objects with mongo java driver v3.0 Released

Context:

If I connect to mongodb how it has described into the mongo java driver documentation, how do I obtain a new GridFS object? There are not a constructor for this signature (MongoDatabase db, String bucket).

MongoCredential credential = MongoCredential.createCredential(_userDb, _database, _passDb.toCharArray());
MongoClient mongoClient = new MongoClient(new ServerAddress(_host, _port), Arrays.asList(credential));
MongoDatabase mongoDatabase = mongoClient.getDatabase(_database);

I would like to avoid use deprecated method. It appears that casting is not possible

GridFS gfsPhoto = new GridFS((DB) mongoDatabase, "photos");

Upvotes: 2

Views: 1263

Answers (2)

Andreas Mattisson
Andreas Mattisson

Reputation: 1061

Could be good to mention. MongoDB version 3.2 it's changed into GridFSBucket. Took me awhile to find the new solution.

// Create a gridFSBucket using the default bucket name "fs"
GridFSBucket gridFSBucket = GridFSBuckets.create(myDatabase);

// Create a gridFSBucket with a custom bucket name "files"
GridFSBucket gridFSBucket = GridFSBuckets.create(myDatabase, "files");

http://mongodb.github.io/mongo-java-driver/3.2/driver/reference/gridfs/

Upvotes: 1

evanchooly
evanchooly

Reputation: 6243

Use mongoClient.getDB(_database). GridFS support in the new API didn't make the schedule for the 3.0 schedule but should be in 3.1. For now you're perfectly safe using the old DB API. It's your only choice, really.

Upvotes: 2

Related Questions