Meteor-Community-Packages / mongo-collection-instances

šŸ—‚ Meteor package allowing Mongo Collection instance lookup by collection name
http://packosphere.com/dburles/mongo-collection-instances
MIT License
73 stars 27 forks source link
collections hacktoberfest instances meteor meteor-package meteorjs mongo mongodb

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.

Installation

$ meteor add dburles:mongo-collection-instances

Usage Example

const Books = new Mongo.Collection('books');

Mongo.Collection.get('books').insert({ name: 'test' });

Mongo.Collection.get('books').findOne({ name: 'test' });

API

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

Returns the collection instance.

Mongo.Collection.getAll()

Returns an array of objects containing:

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:

const connection = DDP.connect('http://localhost:3005');

const Foo1 = new Mongo.Collection('foo'); // local
const Foo2 = new Mongo.Collection('foo', { connection: connection });

Mongo.Collection.get('foo') // returns instance of Foo1

Mongo.Collection.get('foo', { connection: connection }); // returns instance of Foo2

License

MIT, see license file