Model Methods: Distinct

Returning distinct values ​​from a specific field in a collection in MerlinDB.

🪶 The distinct method returns an array of distinct values ​​for a specific field in a collection.

1
  • import
  • MerlinDB,
  • {
  • Schema
  • }
  • from
  • "@chrisaxxwell/merlin-db";
    2
  • //Or if you are using 'merlindb.max.js' just call 'new MerlinDB()';
  • 3
    4
  • const
  • merlin =
  • new
  • MerlinDB(
  • )
  • ;
    5merlin
  • .connect(
  • "USER-DATABASE"
  • )
  • ;
    6
    7
  • const
  • UserSchema =
  • Schema(
  • {
  •   
    8  
  • name:
  • String
  • ,
    9  
  • age:
  • Number
  • 10
  • }
  • )
  • ;
    11
    12
  • var
  • userModel = merlin
  • .model(
  • "User", UserSchema
  • )
  • ;
    13
    14
  • //userModel.distinct(<query>);  
  • 15userModel
  • .distinct(
  • "age"
  • )
  • .then(
  • e =>
  • {
  • 16   console
  • .log(
  • e
  • )
  • ;
    17  
  • //returns: [16, 29, 44, 56 ...]
  • 18
  • }
  • )
  • ;