How do I query for distinct values in Mongoose?

Solution 1:

Just to give an update for Mongoose 3.x:

MyModel.find().distinct('_id', function(error, ids) {
    // ids is an array of all ObjectIds
});

Solution 2:

In my program, this code works.

Person.collection.distinct("born_in_city", function(error, results){
  console.log(results);
});

by node.js v0.4.7, mongoose 1.3.3

Solution 3:

I read through the source code and the node-mongodb-native driver is what powers the class. So on the connection object. So after you have done mongoose.connect(mongodb://), you can give this a shot.

if(mongoose.connections.length > 0) {
  var nativeconn = mongoose.connections[0].conn;
  nativeconn.person.distinct('born_in_city', function(error, results){

  });
}