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.

2
  • 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. Commented Jul 25, 2014 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. Commented Jul 25, 2014 at 23:39

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.