2

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 looks something like this:

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.

Walker D
  • 429
  • 4
  • 7
  • A couple of things. (1) The max is 16MB. I don't know if you made a typo on your post, but it is not GB, but MB. (2) If you're storing a file in MongoDB, you should use GridFS, regardless of its size. It's the best way to store files in MongoDB. – Juan Carlos Farah Jul 25 '14 at 23:10
  • Similar question http://stackoverflow.com/questions/17004242/read-image-file-into-a-mongodb-documents-binary-field-via-mongo-shell-script. Seems like you can't read directly from filesystem to mongodb through the shell. With GridFS you can use mongofiles to insert file data directly from the filesystem. http://docs.mongodb.org/manual/reference/program/mongofiles/. – Robert Munn Jul 25 '14 at 23:39

1 Answers1

1

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"

See mongofiles for more more details.

Community
  • 1
  • 1
Walker D
  • 429
  • 4
  • 7