简体   繁体   English

如何读取文件内容并将其存储在mongo db中

[英]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. 我需要知道是否可以读取文件内容并将内容作为文档存储在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. 您可以对任何熟悉的编程语言使用MongoDB API。

Check out this: http://api.mongodb.com/ 看看这个: http : //api.mongodb.com/

You can add files to GridFS within mongo by using mongofiles.exe from command line. 您可以从命令行使用mongofiles.exe将文件添加到mongo中的GridFS。 ( https://docs.mongodb.com/v3.0/reference/program/mongofiles/ ) 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 我们可以通过运行以下命令和参数将文件放入GridFS存储中

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: 然后,我们可以进入mongo shell,并在fs.files集合中查看文件:

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. MongoDB GridFS还允许您在文件文档上添加额外的元数据,并且还允许额外的索引以使您轻松搜索文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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