How are documents in CollectionFS able to store methods in MongoDB?

107 Views Asked by At

I'm using the Amazon S3 storage adapter with CollectionFS.

Normally you can't store a method into MongoDB. For example, I can't have a document stored in my CustomerOrders collection with a method on it that modifies the document directly with a discount code.

CustomerOrders.findOne().addDiscountCode('SAVE50PERCENT')

But with CollectionFS and the S3 storage adapter I'm able to run a remove function to delete an item from my Images collection as well as delete it from my Amazon S3 bucket:

Images.findOne().remove(function(error, success){
  console.log(success)
});

The similarity in syntax struck me. It's basically saying:

  • Go into the Images collection
  • find a random document
  • return it as an object
  • run the .remove() method that's on that object

However, actually declaring the collection is different.

Instead of doing the following to create my Images collection,

Images = new Mongo.Collection('images');

I have to do:

Images = new FS.Collection("images", {
  stores: [storeName],
  filter: {
    allow: {
      contentTypes: ['image/*']
    }
  }
})

I'm guessing that FS.Collection extended Mongo.Collection's findOne() method so the code can:

  • Go into the Images collection
  • find a random document
  • return it as an object
  • add a .remove() method onto this object
  • return this extended object
  • run the .remove() method that's on that object

Is this kind of correct? The .remove method isn't actually stored in the document itself. It's simply added onto the document object that's returned in findOne()?

1

There are 1 best solutions below

0
On

The remove method comes from a prototype in the FS Collection package (which extends the typical collection capabilities and modifies default behavior), so you are correct. If you take a look at the cfs:collection package, and the api.common.js, you can see exactly how these methods are defined: api.common.js