Get_ID with ObjectID in mongofiles console

Hi, I’m very new and I’m wondering what the syntax is to get a file from GridFS, using the mongofiles console. This doesn’t seem to work:

get_id ‘ObjectId(“5f3f0010c5cd5cc7581728e4”)’ -d MyDatabase

I’ve also tried combinations of single/double quote usage for the ObjectId parameter, with no luck.

Is this even possible?

Hi @Terry_Wray welcome to the community.

For get_id, you need to use the Extended JSON ObjectId format. Using your example:

mongofiles get_id '{ "$oid": "5f3f0010c5cd5cc7581728e4" }'

Since get_id requires the parameter to be a well-formed extended JSON, please ensure that the quote types are correct, i.e. JSON only recognizes double quotes, so "$oid" will work while '$oid' will not.

Best regards,
Kevin

1 Like

@kevinadi, thank you so much for your quick and concise answer!

I’ve now used this command in the mongofiles console:
mongofiles /uri:mongodb+srv://cluster01.myhost.com /username:blah /password:blah /db:myDB put_id “Stairwell logo.png” ‘{"$oid":“b78fe9a0cc83da37e410ac2f”}’

The file was successfully uploaded, but I noticed that the ObjectId is different from a file I uploaded using just the “put” command (letting GridFS assign the ObjectId).
objectId difference

I still seem to be doing something wrong.

Hi @Terry_Wray

I can’t seem to reproduce what you’re seeing:

❯ ~/bin/mongofiles put image.jpg
2020-08-25T13:46:43.480+1000	connected to: mongodb://localhost/
2020-08-25T13:46:43.692+1000	added gridFile: image.jpg

❯ ~/bin/mongofiles put_id image.jpg '{"$oid":"ffffffffffffffffffffffff"}'
2020-08-25T13:47:52.378+1000	connected to: mongodb://localhost/
2020-08-25T13:47:52.404+1000	added gridFile: image.jpg

Inside the fs.files collection, the ids seem to be as expected:

> db.fs.files.find()
{ "_id" : ObjectId("5f4489a3c4819a2eaf45d7f5"), "length" : NumberLong(6781), "chunkSize" : 261120, "uploadDate" : ISODate("2020-08-25T03:46:43.681Z"), "filename" : "image.jpg", "metadata" : {  } }
{ "_id" : ObjectId("ffffffffffffffffffffffff"), "length" : NumberLong(6781), "chunkSize" : 261120, "uploadDate" : ISODate("2020-08-25T03:47:52.392Z"), "filename" : "image.jpg", "metadata" : {  } }

I’m using mongofiles version 100.1.1, in OSX.

Could you double check the version you have? Also, if you’re using Windows, note that the Windows cmd doesn’t recognize single quotes as delimiters. So in Windows the quotes character may need to be escaped like "{^"oid^":^"ffff....^"}"

Best regards,
Kevin

2 Likes

@kevinadi This snip of your code made all the difference: ```
‘{"$oid":“ffffffffffffffffffffffff”}’


Thank, you! That was the format that I needed!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.