Collection2: set allowedValues dynamically from other collection's field?


#1

Is it possible to set Collection2’s allowedValues from another collection’s field?

If so, could I get an example of code that can iterate through all of a collection’s documents and build a deduplicated array for the title field?

Do you think that the array will be dynamic/reactive and if so, how bad would this be for performance?


#2

Did you get an answer to this question. I would also like to see how this would work. I want to store a list of Groups in a collection called Groups. Then in another collection set the allowed values to a field to this same set of groups.

I have tried:

allowedValues: function () {
return Groups.find({})
}


#3

I sort of got this to work by adding the bolded function below. The weird thing though is it only works when the console.log(cn) is included. If I remove it it stops working. Also if I remove autopublish it breaks again. Why does the console.log make a difference? Why does it stop working without the autopublish?

Schemas = {};

Corps = new Mongo.Collection(‘corps’);

Schemas.Corps = new SimpleSchema({
corps: {
type: String,
max:20
}
});

Corps.attachSchema(Schemas.Corps);

corpsNames = function(){
cn = Corps.find().map(function(cn){ return cn.corps });
console.log(cn)
return cn;
};

Divisions = new Mongo.Collection(‘division’);

Schemas.Divisions = new SimpleSchema({
name: {
type: String,
max: 20
},

score: {
type: Number,

},

corps: {
type: String,
allowedValues: corpsNames()
}

});

Divisions.attachSchema(Schemas.Divisions);