Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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.

share|improve this question
    
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 stackoverflow.com/questions/17004242/…. 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. docs.mongodb.org/manual/reference/program/mongofiles. –  Robert Munn Jul 25 '14 at 23:39
    
@JuanCarlosFarah - Thanks, GB was a typo. –  Walker D Jul 26 '14 at 11:07
    
Thanks everyone, I guess I will use GridFS. –  Walker D Jul 26 '14 at 11:07

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.