Cursor Methods: Limit

Limiting the number of documents returned in a collection in the database in MerlinDB.

🪶 The
  • .limit
  • method is used to restrict the number of documents returned in a query.

    Setting cursor.limit( { 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(
  • )
  • ;
    5merlin
  • .connect(
  • "USER-DATABASE"
  • )
  • ;  
    6
  • var
  • userModel = merlin
  • .model(
  • "User", <userSchema>
  • )
  • ;
    7
    8
  • //cursor.limit(<number>);  
  • 9userModel
  • .find(
  • {
  • name:
  • "Sam"
  • }
  • )
  • 10
  • .limit(
  • 3
  • )
  • 11
  • .then(
  • e =>
  • {
  • 12  
  • //Returns all '3' first documents whose name is 'Sam'
  • 13
  • }
  • )
  • If used with the cursor.skip() method, it limits the documents from the skip.