2

I wanna upload small files of size lesser than 16MB to MongoDB via BinData type that i came to know is the only default option for smaller files whereas GRIDFS is ideally used for files exceeding 16MB in size.

Unfortunately I didn't easily get proper documentation and examples of uploading files without GridFS on MongoDB docs. The information I found about BinData type is either quite limited or I failed to understand. Going through several similar questions here (that are mostly python based) and elsewhere, I got some idea about usage of BinData but still I'm unable to successfully upload the files in this way.

I need more information about uploading files via BinData and especially the right way to initialise as I usually get BinData not a function or BinData is not defined errors. Here's my current code where I'm testing the functionality:

import { Meteor } from "meteor/meteor";
import { Mongo } from "meteor/mongo";
export const Attachment = new Mongo.Collection("attachment");
let BinData = Mongo.BinData; //wrong initialisation

function createAttachment(fileData) {
  const data = new Buffer(fileData, "base64");
  Attachment.insert({file: new BinData(0, data)});
}

Some helpful links:

BSON Types in Mongo

BSON spec

Adil
  • 21,278
  • 7
  • 27
  • 54
  • 1
    Don't have a moment to spin up an instance and test, but according to the [README in the repo](https://github.com/meteor/meteor/tree/devel/packages/mongo#direct-access-to-npm-mongodb-api) you need to access this from `MongoInternals` instead, and gain direct access to the full imported module. It's not exported on `Mongo`. Failing that you can always require the indepedent module. – Neil Lunn Sep 05 '17 at 12:01
  • 1
    GridFS can be used for smaller files too, it's not a restriction, an given that it's supported directly, I would opt for that – Mikkel Sep 05 '17 at 12:59
  • @Mikkel can you please share any example? – Adil Sep 06 '17 at 06:04
  • @Mikkel I'd found a very simple example [here (as in option1)](https://stackoverflow.com/questions/27934141/meteor-uploading-file-from-client-to-mongo-collection-vs-file-system-vs-gridfs?answertab=active#tab-top) but it just saves `BinData(0,"")` and displays nothing while reading the data from mongodb. – Adil Sep 11 '17 at 11:36
  • I guess these techniques are possible, but you are trying to stay low level (presumably you don't like using packages), but you buy yourself all kinds of hardship as opposed to just using a package and continuing to be productive. That's the benefit of using packages, and the reason why npm is so popular – Mikkel Sep 11 '17 at 13:43
  • You're right somehow. I love using popular packages like npm that are really playing big part in development but I usually try my best to avoid using several third party less popular libs or modules in a simple app or module that can easily be achieved without using these libs. I'd thought this would be quite basic thing like 4-5 lines of code we use to save files to disk. That's the reason I actually wanna avoid adding an additional dependency for just 4-5 lines of code. – Adil Sep 11 '17 at 14:33

1 Answers1

1

There are several Meteor Packages that you can use for file uploading.

I have used this one myself https://atmospherejs.com/vsivsi/file-collection

It can store your files in gridfs, and provides urls for retrieving images etc

Also:

https://atmospherejs.com/jalik/ufs https://atmospherejs.com/ostrio/files

Mikkel
  • 7,693
  • 3
  • 17
  • 31
  • So the functionality can't be achieved without these third party libraries in meteor mongo, right? – Adil Sep 06 '17 at 07:08
  • 1
    It probably can, you could look at the code and work out what you need, but it's a lot less work to use a package that has been tried and tested – Mikkel Sep 06 '17 at 07:18