Retrieve distinct values from backend mongo


I have a Collection such as the one stated below:

db.Defaults.find() :

{ "_id" : ObjectId("576a2c10d24f5154fd65d3c2"), "schemaName" : "A_schema", "label" : "time-enable", "value" : "yes" }
{ "_id" : ObjectId("576a2c20d24f5154fd65d3c3"), "schemaName" : "A_schema", "label" : "time-interval", "value" : 30 }
{ "_id" : ObjectId("576a2c29d24f5154fd65d3c4"), "schemaName" : "A_schema", "label" : "keep-alive-count", "value" : 3 }
{ "_id" : ObjectId("576a2c33d24f5154fd65d3c5"), "schemaName" : "B_schema", "label" : "preference", "value" : 100 }
{ "_id" : ObjectId("576a2c45d24f5154fd65d3c6"), "schemaName" : "B_schema", "label" : "timeout", "value" : 30 }
{ "_id" : ObjectId("576a384ad24f5154fd65d3cb"), "schemaName" : "C_schema", "label" : "enable", "value" : "no" }
{ "_id" : ObjectId("576a391ed24f5154fd65d3cc"), "schemaName" : "D_schema", "label" : "policy", "value" : "no" }

I need to display distinct schemaName in a dropdown using meteor.

When I try the below, I’m able to retrieve all the schemaNames, except that there are duplicate names since one schemaName has multiple label/value pairs.

"schemaName": {
        type: String,
            options: function () {
                return Defaults.find({},{schemaName:1}).map(function (c) {
                    return {label: c.schemaName, value: c.schemaName}; });

I know mongo has a distinct function such as :


But when I use it in the autoform, the form fails to render the dropdown. I get the following message in the console.

Defaults.distinct is not a function at Object.options

Is there a way to combine distinct with find in mongo?


When you set this as the value for options, did you just add it directly (like Default.distinct) or wrap it in a function? From the error message it looks like you’re missing the wrapping function.

That being said, distinct is not supported by minimongo, so this approach won’t work unfortunately. Your best bet is to handle this outside of your collection. Retrieve your collection results as an array then use something like _.uniq.