简体   繁体   中英

How to read file contents and store it in mongo db

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.

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM