[JAVA-1983] GridFSBucket cannot use a custom id Created: 30/Sep/15  Updated: 19/Oct/16  Resolved: 01/Oct/15

Status: Closed
Project: Java Driver
Component/s: GridFS
Affects Version/s: 3.1.0
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: yuechao Assignee: Ross Lawley
Resolution: Done Votes: 0
Labels: feature
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Minor Change

 Description   

with the old GridFS api, I can use an string uuid generated from java for the file id.
GridFSInputFile.setId("<a string uuid>")
with the new GridFSBucket api, it seems that I have to use an ObjectId generated by the api.



 Comments   
Comment by Ross Lawley [ 01/Oct/15 ]

Hi youzai001,

Thanks for the follow up, the new GridFS implementation follows the cross driver GridFS Specification . That specification has the expectation that the driver will generate the ObjectId, as such the Java implementation does indeed work as designed. I suppose having the driver generate the _id reduces the risk of there being a duplicate key exception compared to a user supplied _id.

The GridFSUploadOptions.metadata is designed to store any associated metadata that you require, so it looks like using that could be a good fit. Alternatively, you could store the file_id in the associated document which may be beneficial but you would have to ensure it stays in sync with gridfs if there are an revisions. However, a unique filename might be the best solution here as the downloadToStreamByName methods can be used to ensure you always get the latest version of a given file.

I hope that gives you some reasonable options to go forward with. If after reading this you feel like not allowing user generated file ids is a mistake, then the way to get the Specification reviewed for all drivers would be by creating a ticket in the DRIVERS project.

I'd be interested in your thoughts,

All the best,

Ross

Comment by yuechao [ 01/Oct/15 ]

Hi Ross Lawley,

But I can't use an ObjectId generated by my java code too. Can you implement this?

This is usable sometimes when I want to associate a document with a file, I can just set the file id to the document id.

if not, I have to add a spare id to my document.

Comment by Ross Lawley [ 30/Sep/15 ]

Hi youzai001,

Thats correct, the new GridFS Specification deprecated using alternative types for _id fields.

Whilst we still support legacy _id types via the download methods we no longer allow custom _id types when uploading for the new API.

As not adding a new but deprecated method in the new API for the upload methods was a conscious decision, I'm closing this ticket as works as designed.

I hope that clarifies the situation.

Ross

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