How can one add text files in Mongodb?

只谈情不闲聊 提交于 2019-12-23 04:19:12

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/13081660/how-can-one-add-text-files-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!