0

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 
Dev
  • 13,492
  • 19
  • 81
  • 174
The6thSense
  • 8,103
  • 8
  • 31
  • 65
  • Need quite a bit more information to help, like your driver/language etc etc – Sammaye Nov 14 '14 at 10:02
  • Welcome to Stackoverflow. This community is aimed at questions about programming problems. Please read the [tour for details](https://stackoverflow.com/tour). – Markus W Mahlberg Nov 14 '14 at 10:03
  • Now how are you storing your files? are you using `mongofiles` command – Praveena Nov 14 '14 at 10:53
  • yeah @Praveen i am using mongofiles method to insert into the db – The6thSense Nov 14 '14 at 11:03
  • By default mongo will provide an id to your stored file.. Do you want to cahnge it? – Praveena Nov 14 '14 at 11:05
  • Both of these things are not features of mongofiles, which is a simple utility for inserting an retrieving files with GridFS. To be able to set the `_id` and other fields at insertion and query the GridFS collections with full MongoDB query syntax, use a driver that support GridFS, like the official Java or Python drivers. How do you want to search the content? It is stored as binary data so it may not be searchable in the way that you desire. – wdberkeley Nov 14 '14 at 16:59
  • Thanks @wdberkeley so i there no way to check the content of file stored by gridfs method – The6thSense Nov 17 '14 at 04:53
  • There's no way to query the content with `mongofiles`. With a driver you can add and query metadata and query content (binary content) and use those searches to retrieve files or parts of files. You can't semantically search the content of the files with either `mongofiles` or a driver since that requires the file- or application-specific knowledge to read the file in the proper way. – wdberkeley Nov 17 '14 at 06:45
  • Thanks @wdberkeley you solved my doubt .Can u provide any link or code sample for inserting files with user defined id using python driver – The6thSense Nov 17 '14 at 11:38

2 Answers2

0

To read your file

./mongofiles get file.txt
 cat file.txt
Praveena
  • 6,340
  • 2
  • 40
  • 53
0

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" )
The6thSense
  • 8,103
  • 8
  • 31
  • 65