How to read file contents and store it in mongo db

2.9k views Asked by At

I am in the need of to know is it possible to read the file content and store the contents as documents inside a collection in mongodb. If it s possible how to do that? If its not possible do there exist alternative way of doing that? Thanks in advance.

2

There are 2 answers

0
Kevin Smith On

You can add files to GridFS within mongo by using mongofiles.exe from command line. (https://docs.mongodb.com/v3.0/reference/program/mongofiles/)

We can put a file within the GridFS store by running the following command and arguments

C:\mongodb\bin>mongofiles.exe put \temp\Music.mp3
2016-12-17T10:05:05.048+0000    connected to: localhost
added file: \temp\Music.mp3

We can then drop in to the mongo shell and see the file within the fs.files collection:

C:\mongodb\bin>mongo.exe
MongoDB shell version: 3.2.10
connecting to: test
> show collections
fs.chunks
fs.files
> db.fs.files.find().pretty()
{
        "_id" : ObjectId("58550dd18f96a237504521b3"),
        "chunkSize" : 261120,
        "uploadDate" : ISODate("2016-12-17T10:05:05.143Z"),
        "length" : 5386368,
        "md5" : "0ed99265aaf3a64ddf5df794e98b1cb3",
        "filename" : "\\temp\\Music.mp3"
}
>

MongoDB GridFS also allows you to add extra meta data on the file document and allows extra indexes too to allow you to easily search for documents.

0
notme On

You can use MongoDB APIs for whatever programming language that you're familiar with.

Check out this: http://api.mongodb.com/