user3736968
user3736968

Reputation: 85

Distinct in sails js and mongodb

Model.distinct("Age").done(function(err, ward) {
  if(err)
    return next(err);
  if(!ward)
    return next();
  res.json(ward);
});

After executing this code, it gives the following error:

TypeError: Object [object Object] has no method 'distinct'

But distinct works in robomongo. How can I rectify the error?

Upvotes: 4

Views: 2794

Answers (2)

mdunisch
mdunisch

Reputation: 3697

Currently Walterline (the ORM of sails) dont support distinct()-function.

But you can use the native()-function to get direct access to the native mongo driver:

Modelname.native(function(err,coll){
  coll.distinct("Age", function(err,result){
     res.json(result);
  });
});

See: https://sailsjs.com/documentation/reference/Models/Model-Methods/native.html

Upvotes: 3

Aurélien Thieriot
Aurélien Thieriot

Reputation: 5923

What you probably need is the ability for the SailsJS ORM (Waterlin) to give you back the native MongoDB collection: http://beta.sailsjs.org/#/documentation/reference/Models/Model-Methods/native.html

Once you've got it you will be able to call native MongoDB queries.

By design, Waterline is meant to be used with several databases while keeping the same code. And currently there is no "disctinct" feature.

Upvotes: 0

Related Questions