Question

C:\Programs\MongoDB\bin>mongofiles.exe list
connected to: 127.0.0.1
test123.txt     6
test123.txt     22
test123.txt     44
test456.txt     53
test456.txt     53
test456.txt     53
test456.txt     53
test456.txt     53
test456.txt     15

C:\Programs\MongoDB\bin>

When I do mongofiles get test456.txt I always get the last
file which I put with that name in GridFS. How do I get the
other/previous ones provided they all have duplicate names?

According to the documentation I need to specify:

"A filename which is either: the name of a file on
your local's file system, or a GridFS object."

http://docs.mongodb.org/manual/reference/program/mongofiles/

But it's not really clear to me what "a GridFS object" means in this context.

Was it helpful?

Solution

Well a "GridFS object" in this context means the name of the object stored in the GridFS collections. The distinction is possibly best explained by the --local option on the mongofiles manual page.

What you need to consider here is that what you have done is submit items with the "same" filename using the mongofiles utility. As noted in the manual page for that utility, the default behavior for the put (see commands) option is to create a new entry within the store. This can be overridden with the --replace option so that any existing content is found and overwritten with the new content you have created.

In short, regardless of whether the files contain different content or not, you have created several things with the same "Object name". As far as the mongofiles utility is concerned, it only knows how to fetch by the "Object name" so it will just retrieve the first one it finds, by it's rules.

Now in most API implementations of GridFS, the actual get operations are typically done by _id. Every "Object" you created in this way does still have it's own unique _id value, so when this is applied then you can get the "Object" that you want.

Some API implementations add additional query type methods in order to find by "filename" or other meta data information. But mostly they do not bother as these are really just standard .find() or .findOne() operations on whatever collection hold the content meta-data and references ( fs.files by default ). This provides a more than reasonable amount of ways to "find" a particular object and issue that _id value through the get interface of that API.

So while mongofiles is a nice utility for doing basic CRUD type operations from the command line, it merely is a utility and not the main implementation of "how to do it". So as a utility it provides a "convenience" form of setting and getting objects, being using the "filename" part of the Object identifier.

Also worth noting is that you should really treat a GridFS store just like a filesystem, and keep your "filenames" unique just as would be required in a filesystem.

But as for the mongofiles utility, the "name" is all you have to go on to retrieve information. Try not to do that, or really use your chosen language API to do the work instead.

OTHER TIPS

@Neil Lunn answer is correct.

Here is my example:

-d means your database,

-l means your local file system path to the file you will upload

put means you want to upload a file

CDBG_2015_1.pdf means the file name in GridFS( Not your local file system path )

 mongofiles -d civilpdf -l  C:\jh1\share_map_online\desktop_map_export\pdf\CDBG_2015_1.pdf put CDBG_2015_1.pdf

Then you could search file by keyword "CDBG"

mongofiles -d civilpdf search CDBG

You could download file to a whatever place you like as

mongofiles -d civilpdf -l "C:\your liked whatever place"  get CDBG_2015_1.pdf

I know this question is quite old, but people may still get here in search of an answer, so I thought to add my 2 cents.

It is probably a new feature in more recent versions of mongofiles, but now you can let the mongofiles utility work with an object id also, so not just filename anymore. To do so, you can e.g. use the following command:

mongofiles get_id 'ObjectId("xyz...")'

where xyz... is the mongo DB object ID string.

Now unfortunately, I can't see a way in which mongofiles can be used to display the object ids of the files in the DB, so for that, you'll need to use the normal mongo command line interface (mongo) with a command such as

db.getCollection("fs.files").find({filename:/abc/i})

where abc is the name of the file. The output will look something along the lines of

{ "_id" : ObjectId("xyz..."), "chunkSize" : 261120, "uploadDate" : ISODate("2018-12-09T13:37:43.743Z"), "length" : 16904, "md5" : "08ea...", "filename" : "abc" }

If there are multiple files with the name abc, there will be multiple items in the result. From here you can get the object id string and then use that for invoking the mongofiles get_id command...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top