Skip to content

Latest commit

History

History
67 lines (46 loc) 路 2.22 KB

collection-instances.md

File metadata and controls

67 lines (46 loc) 路 2.22 KB

FilesCollection Instances and Mongo.Collection Instances

While FilesCollection has a direct reference to a Mongo.Collection, the Mongo.Collection has a back-reference to the FilesCollection itself.

The reference chain is as the following:

const imagesCollection = new FilesCollection({collectionName: 'images'});

// get the underlying Mongo.Collection
const collection = imagesCollection.collection;

// get the 'parent' FilesCollection
// of this collection instance
const parent = collection.filesCollection;

// returns true
console.log(parent === imagesCollection);

Using dburles:mongo-collection-instances

In Meteor for Mongo.Collection instances management, we suggest to use dburles:mongo-collection-instances. It allows to retrieve Mongo.Collection instances by its name in any module or file.

Note: This package comes with no dependency to dburles:mongo-collection-instances. To make use of it, install it to your Meteor project first.

meteor add dburles:mongo-collection-instances

Then initialize FilesCollection, which itself will create a new Mongo.Collection, that is automatically added to a hidden Mongo.Collection instances list.

const imagesCollection = new FilesCollection({collectionName: 'images'});

To retrieve FilesCollection use - Mongo.Collection.get('collectionName'):

const ImagesCollection = Mongo.Collection.get('images');
const imagesCollection = ImagesCollection.filesCollection;

Using a custom collection instance management

This simplified example shows, how to make use of that technique in your own implementation. Assume having a map of all Mongo.Collection instances:

const collectionsMap = {};

Since you may not want to store the FilesCollection instance (because it is not a Mongo.Collection), you can still reference the underlying Mongo.Collection:

const imagesCollection = new FilesCollection({collectionName: 'images'});
collectionsMap.images = imagesCollection.collection;

Access the FilesCollection by reference:

const imagesCollection = collectionsMap.images.filesCollection;