MongoDb gridfs method

410 Views Asked by At

i have two doubts .they are

1.How to provide object id when storing file using gridfs

the command that i use is mongo file

  mongofiles -dbpath gridfs put hi.txt

2.Can we view the content of file which was stored in mongodb using gridfs method

  I want to QUERY THROUGH the content of the stored gridfs files is it possible 
2

There are 2 best solutions below

0
On BEST ANSWER

For the first question you could use the following sample code for providing "_id" and "filename" during insert time

import sys
import pymongo
import pymongo.errors
import gridfs
def main():
    '''CONNECTING TO PYMONGO'''
    try:
        c=pymongo.Connection(host="localhost",port=27017)
        print "Connection is successfull"
    except pymongo.errors.ConnectionFailure ,e:
       sys.stderr.write("Could not connect to MongoDB: %s" % e)
       sys.exit(1)
    db=c["family"]
    fs = gridfs.GridFS( db )
    w=open( 'as.txt', 'r')
    fileID = fs.put( w ,_id="a1",filename="a.txt" )
1
On

To read your file

./mongofiles get file.txt
 cat file.txt