如何使用JavaScript将二进制文件插入mongodb? [英] How do I insert a binary file into mongodb using javascript?
问题描述
我想使用JavaScript编写脚本,以将二进制文件插入mongodb.该文件远远小于最大16MB限制,因此可以通过 GridFS 超出了我正在编写的功能范围.
I want to write a script using javascript to insert a binary file into the mongodb. The file is far less than the max 16MB limit, so implementing this with GridFS is beyond the scope of the feature I'm writing.
我正在编写脚本,因此可以像这样运行它:
I'm writing the script so I can run it like so:
mongo localhost:27017/myMongoDB loadMyFile.js
loadMyFile.js
看起来像这样:
db.myCollection.insert( {
fileName: "myFile.ogg",
file: cat(./myFile.ogg")
});
但是当我搜索集合时:
db.myCollection.find({
fileName:"myFile.ogg"
});
我得到类似的东西:
{
"_id":ObjectId("53d2d6c76c694a37315c0195"),
"fileName": "myFile.ogg",
"file":"ID3\u0002"
}
这似乎是错误的.请协助.谢谢你.
Which seems wrong. Please assist. Thanks in advanced.
推荐答案
如@ JuanCarlosFarah 和@ RobertMunn ,GridFS似乎是最好的(也许是唯一的?)无需编写小型应用程序即可将文件插入db的方法.
As mentioned in the comments by @JuanCarlosFarah and @RobertMunn, GridFS appears to be the best (and maybe the only?) way to insert files into the db without writing a small application.
mongofiles -d myMongoDB -l myFileName.ogg --type audio/ogg put "The Name of my File in the Collection"
有关更多详细信息,请参见 mongofiles .
See mongofiles for more more details.
这篇关于如何使用JavaScript将二进制文件插入mongodb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!