Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieving the last inserted ObjectId or Filename #4

Open
Andargor opened this issue May 27, 2017 · 3 comments
Open

Retrieving the last inserted ObjectId or Filename #4

Andargor opened this issue May 27, 2017 · 3 comments
Assignees

Comments

@Andargor
Copy link

I would like to retrieve the ObjectId (or even filename) from the callback, and this even if there was an error. At least, I have not figured out how to do so. To illustrate, this is what I am trying to achieve.

I would like to have efficient, implicit deduplication when a document is inserted in GridFS. My root name is "attachments", for this example. To do so, I have created a unique index on the "md5" field in the attachment.files collection. This means that there should be an error if a filename with the same md5 hash is inserted. And I can then handle it, although I don't know how to retrieve the ObjectID or md5 hash from this transaction.

There is a problem with this: when the document is inserted, It is first created in attachments.chunks, and then hashed, and only then the attachment.files entry is attempted to be created, and raises the error if there's a duplicate. Which means there are dangling chunks with no corresponding files entry. Again, if I could know what the ObjectId was at the moment the attempt was made, I could handle it and simply remove the duplicate chunks. How do I do this?

My workaround is a naive approach where I manually md5 hash the file before and checking if it exists before attempting to insert, but that seems to me inefficient, since it will be hashed a second time.

@JonatanSalas
Copy link
Member

JonatanSalas commented May 28, 2017

@Andargor so you want a callback to be exposed for the handleFile and removeFile functions giving you the ObjectId of the file been inserted/removed?
If this is true, I will work on adding support for it!

@Andargor
Copy link
Author

@JonatanSalas Yes, getting the ObjectId whether the insert succeeds or fails would satisfy the request so there can be cleaning up on an error. Or setting it in advance so it can be used to handle the error.

In the meantime I've implemented my "naive" approach, so this is not an emergency. :)

@JonatanSalas
Copy link
Member

@Andargor great! I will work to add support for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants