简体   繁体   English

如何使用javascript将二进制文件插入mongodb?

[英]How do I insert a binary file into mongodb using javascript?

I want to write a script using javascript to insert a binary file into the mongodb. 我想使用JavaScript编写脚本,以将二进制文件插入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. 该文件远远小于最大16MB的限制 ,因此使用GridFS实现此功能超出了我正在编写的功能的范围。

I'm writing the script so I can run it like so: 我正在编写脚本,因此可以这样运行它:

mongo localhost:27017/myMongoDB loadMyFile.js

loadMyFile.js looks something like this: loadMyFile.js看起来像这样:

db.myCollection.insert( {
  fileName: "myFile.ogg",
  file: cat(./myFile.ogg")
});

But when I search the collection: 但是当我搜索集合时:

db.myCollection.find({
    fileName:"myFile.ogg"
});

I get something like: 我得到类似的东西:

{
  "_id":ObjectId("53d2d6c76c694a37315c0195"),
  "fileName": "myFile.ogg",
  "file":"ID3\u0002"
}

Which seems wrong. 好像错了 Please assist. 请协助。 Thanks in advanced. 提前致谢。

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. 如@ JuanCarlosFarah@RobertMunn的评论中所述,GridFS似乎是在不编写小型应用程序的情况下将文件插入db的最佳方法(也许是唯一方法?)

mongofiles -d myMongoDB -l myFileName.ogg --type audio/ogg put "The Name of my File in the Collection"

See mongofiles for more more details. 有关更多详细信息,请参见mongofiles

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

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