Cursor Methods: Sort
Ordering documents from a collection in the database in MerlinDB.
🪶 The sort method is used to sort the documents in a collection based on specified criteria.
Setting cursor.sort( { name: 1 } ) causes the fields to be sorted in ascending order by name, if using -1 sorts in descending order.
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( ) ;5 merlin.connect( "USER-DATABASE") ; 6 var userModel = merlin.model( "User", <userSchema>) ;7 8 //cursor.sort(<object>); 9 userModel.find( { name: "Sam"} ) 10 .sort( { age: 1 , name: -1 } ) 11 .then( e => { 12 //Returns the documents in ascending order by 'age' and in descending order by 'name'; 13 } )