manida
manida

Reputation: 41

How can one add text files in Mongodb?

I have certain requirement to insert a text file in mongodb, retrieve it back and then check that whether files are same. I am hoping to do it without GridFS as the files i want to use is lesser than 16MB, so can you guys plz suggest me ways to do it, considering that set up of mongodb is very basic one.

Thanks

Upvotes: 1

Views: 6220

Answers (2)

osburnsharp
osburnsharp

Reputation: 211

A text file that is less than 16 MB can be stored as a simple key-value pair in a plain document. No need for GridFS and no need for Binary or JSON objects. If in doubt, try it. It works. Read the contents of the file into a variable and store it against a key such as, I don't know, 'data'. Retrieve the object and write the value of the 'data' key to whatever you like. Line breaks are all preserved so no need to do anything within the file. Really simple.

Upvotes: 2

ramazan polat
ramazan polat

Reputation: 7900

You can insert JSON objects or binary files (both stored as BSON objects) to mongodb.

If you want to insert content of a text file, than you can insert it as a JSON object.

But if you want to insert a text file or binary file, you have to use GridFS.

There is no way to insert a file to mongodb without GridFS unless you split the file into chunks and store as JSON object which is what GridFS already doing.

Upvotes: 0

Related Questions