-1

I am new in mongo db, i just wanted to know that how can i handle the images along with texts in mongo db?, i have a post collection and, am inserting datas to it but i dont know how to insert both, means texts and images.

Naveenbos
  • 2,532
  • 3
  • 34
  • 60
  • This is too broad as written and is asking for a tutorial. Please add code showing what you've tried and where you've gotten stuck. There are many resources on the internet available discussing files with PHP and Mongo that should help get you started. – WiredPrairie Mar 01 '14 at 12:49

1 Answers1

0

Your question would do better with an example, but the short answer is MongoDB does not care, it will just deal with it, so just insert it.

With a little more explanation, what will actually happen is your language driver will take care of that, and make sure that when you post something binary for instance, then that will send it on in the correct BSON format so it is also stored that way in MongoDB.

The same goes in reverse where when retrieving the data, your driver will do the translation into what is appropriate for your language.

So consider the pseudo-code ( sort of ) here:

var binData = <containing an image file data or something>;

db.collection.insert({
    name: "Gary",
    hobies: ["Football", "Tennis", "Xbox"],
    profilePic: binData
});

And that will just create a document in your collection. Read it back and the profilePic field is just binary data you would use just like any other variable.

The only limitation is the 16MB BSON Document Size which applies to any document you want to store with MongoDB. If you are hitting this limitation, then your driver should provide and implementation for GridFS which is the MongoDB implementation for dealing with documents over this size.

Even though some people mistakenly believe that GridFS is how you store a file with MongoDB. That is false, it's just all data as far as Mongo is concerned.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317