简体   繁体   中英

Querying mongodb gridfs file data

hi i am a newbie to gridfs and am able to insert a file and view the file in gridfs using the query below

mongofiles -d myfiles put hi.txt

db.fs.files.findOne({'filename':'hi.txt'});

I need to view the contents of the file(hi.txt) i tried getResources but it didnt seem to work.Am stuck here,any help will be much helpful

As far as I know, in nodeJS (though you didn't specifically mentioned it in your question, just tagged it), the GridStore object is to be used for manipulating the files:

(quoted form the GridStore doc )

Opening a GridStore (a single file in GridFS) is a bit similar to opening a database. At first you need to create a GridStore object and then open it.

 var gs = new mongodb.GridStore(db, filename, mode[, options])

Reading from GridStore can be done with read

 gs.read([size], callback)

where

  • size is the length of the data to be read
  • callback is a callback function with two parameters - error object (if an error occured) and data (binary string)

Streaming from GridStore:

You can stream data as it comes from the database using stream

gs.stream([autoclose=false])

where

  • autoclose If true, current GridStore will be closed when EOF and 'close' event will be fired

The function returns read stream based on this GridStore file. It supports the events 'read', 'error', 'close' and 'end' .

Also, at the quoted doc site, there are a lot of useful examples for storing file, etc...

Recommended reading:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM