Get collection name from document

Is there a way to get the collection name from a document? So doing
Meteor.users.findOne().getCollectionName() should return users.

How/ is that possible?

dburles:mongo-collection-instances is a great package that allows you to get a Mongo.Collection instance if you’ve got the name of the collection. But I don’t think it’s going to help get the name of a collection, given a document.

Would be nice if this was possible, but I don’t think it is.

Thanks for your responses so far, just had a look at dburles:mongo-collection-instances but it doesn’t allow me to do what I want here …

Have you tried the _name property of a collection?

> Meteor.users._name

@jamgold That works, but only on the collection - not its documents.

@fvg You could use transform to add this:

Basically, you could attach the collection name as a field on the prototype for every returned object.

You can add a helper that returns collection name with this awesome package

@yasinuslu Thanks, it’s what @sashko just proposed. However, this is just what I want: Get the collection name inside a collection helper/ transform. I’ve now found a solution for my case. Kinda ugly, but works:

createUser = (name) ->
  -> console.log "Create new user for document #{@_id} of collection #{name}"

App.hooks.add 'collections:on:startup', (collection, name) ->
  collection.helpers { createUser: createUser(name) }

// Edit: Quick

var createUser;

createUser = function(name) {
  return function() {
    console.log("Create new user for document " + this._id + " of collection " + name);

App.hooks.add('collections:on:startup', function(collection, name) {
    createUser: createUser(name)

For anyone else that needs this feature… I implemented it like this:

Meteor.startup(function () {
	for (var obj in this) {
		var collection = this[obj];
	    if (collection instanceof Mongo.Collection) {
	        collection._transform = (function (doc) {
	    		var self = this;
	    		var previousTransform = doc;
	    		if (collection._transform && (typeof collection._transform === 'function'))
	    			previousTransform = self.transform(doc);
				return _.extend(previousTransform, {
					getCollectionName: function () {
						return self.collectionName;
	        	collectionName: collection._name,
	        	transform: collection._transform
1 Like

Could try out my socialize:base-model package.

var Thing = BaseModel.extendAndSetupCollection("things");

var thingInstance = Meteor.things.findOne();

thingInstance.getCollectionName() //=> "things"

If you need this functionality on users then the socialize:user-model package will satisfy this requirement

meteor add socialize:user-model

Meteor.user().getCollectionName()  //=> "users"
1 Like

You can use the this package ivansglazunov:dbrefs.

Test = new Meteor.Collection('test');
Test.insert({ _id: 'a' });
var dbref = Test.findOne('a').DBRef() // { $ref: 'test', $id: 'a' }
Meteor.Collection.get(dbref.$ref) == Test // true
var document = DBRef(dbref); // { _id: 'a' }



This looks exactly what I would need. Unfortunately I get

"TypeError: Property ‘transform’ of object [object Object] is not a function "

in line

“previousTransform = self.transform(doc);”

Any idea?


Or you can just use or

anyDocument.Collection() // String
anyDocument.Ref() // { id: String, collection: String }