Skip to content

Instantly share code, notes, and snippets.

@tim-hub
Created January 25, 2017 07:44
Show Gist options
  • Save tim-hub/d2c2a60379073e48b916e097aac81995 to your computer and use it in GitHub Desktop.
Save tim-hub/d2c2a60379073e48b916e097aac81995 to your computer and use it in GitHub Desktop.
mongodb use gridfs pymongo
#!/usr/bin/env python
import pymongo
import gridfs
# establish a connection to the database
connection = pymongo.MongoClient()
#get a handle to the test database
db = connection.test
file_meta = db.file_meta
file_used = "sample_128_mb.txt"
def main():
grid = gridfs.GridFS(db, "text")
fin = open(file_used, "r")
_id = grid.put(fin)
fin.close()
print "id of file is", _id
file_meta.insert( { "grid_id": _id, "filename": file_used})
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment