dburles:mongo-collection-instances

v1.0.0-beta300.2Published 2 days ago

Meteor Mongo Collection Instances

Meteor Community Package Test suite CodeQL Project Status: Active – The project has reached a stable, usable state and is being actively developed. JavaScript Style Guide

This package augments Mongo.Collection (and the deprecated Meteor.Collection) and allows you to later lookup a Mongo Collection instance by the collection name.

Version 1.0.0 breaking changes!

Beginning with version 1.0.0, this packages requires Meteor >= 3.0

Apart from that, everything else remains the same.

Installation

$ meteor add dburles:mongo-collection-instances

Usage Example

1const Books = new Mongo.Collection('books');
2
3Mongo.Collection.get('books').insert({ name: 'test' });
4
5Mongo.Collection.get('books').findOne({ name: 'test' });

API

Mongo.Collection.get('name', [options])

Returns the collection instance.

  • name (String)
  • options (Object) [optional]
    • options.connection (A connection object, see example below)

Mongo.Collection.getAll()

Returns an array of objects containing:

  • name (The name of the collection)
  • instance (The collection instance)
  • options (Any options that were passed in on instantiation)

Multiple connections

It's possible to have more than one collection with the same name if they're on a different connection. In order to lookup the correct collection instance, you can pass in a second argument. An object containing the connection object.

For example:

1const connection = DDP.connect('http://localhost:3005');
2
3const Foo1 = new Mongo.Collection('foo'); // local
4const Foo2 = new Mongo.Collection('foo', { connection: connection });
5
6Mongo.Collection.get('foo') // returns instance of Foo1
7
8Mongo.Collection.get('foo', { connection: connection }); // returns instance of Foo2

License

MIT, see license file