Reputation: 2486
In ruby using the mongo gem I can't find any documentation on how to find on a filename with GridFs.
Upvotes: 2
Views: 1190
Reputation: 434755
First get a connection to the database, we'll call this db
. Then you can connect to your GridFS as a Mongo::Grid or Mongo::GridFileSystem instance:
fs = Mongo::Grid.new(db)
fs = Mongo::GridFileSystem.new(db)
Now you can use the Mongo::GridExt::InstanceMethods methods on fs
. In particular, you can use exist?
:
f = fs.exist? :filename => 'pancakes.png'
The exist?
method is poorly named as it gives you a Hash if it finds something and a nil
if it doesn't.
That's not terribly useful if you're searching for, say, all filenames that match /pancakes/
. However, GridFS is just a pair of normal MongoDB collections:
fs.files
: the file metadata.fs.chunks
: the file data (in chunks).If you want to do arbitrary metadata searches then you just need to get your hands on fs.files
and have your way with it:
fs = db['fs.files']
cursor = fs.find(:filename => /pancakes/)
# Now iterate through cursor or .count it or ...
The fs
above will be a Mongo::Collection so its find
method accepts all the usual query options.
Upvotes: 7