monSQLize chain calling method support documentation

Overview

Now you can build queries using chained calls just like you would with the native MongoDB driver. Chained methods fully support caching, parameter validation and error handling.


🎯 Supported chain calling method (completely implemented)

1. find() method

✅ Supported chain calls (12 methods in total)

MethodsSyntaxDescriptionExposed capabilities
.limit(n).limit(number)Limit the number of returned documentsfind() query chain
.skip(n).skip(number)Number of skipped documentsfind() query chain
.sort(spec).sort(object)Sorting rulesfind() query chain
.project(spec).project(object)Field projectionfind() query chain
.hint(spec).hint(object|string)Index promptfind() query chain
.collation(spec).collation(object)Sorting rulesfind() query chain
.comment(str).comment(string)Query commentsfind() query chain
.maxTimeMS(ms).maxTimeMS(number)Query timeoutfind() query chain
.batchSize(n).batchSize(number)batch sizefind() query chain
.explain(v).explain(string?)Return query execution planfind() query chain
.stream().stream()Return streaming resultsfind() query chain
.toArray().toArray()Explicit conversion to arrayfind() query chain

limit(0) intentionally keeps the MongoDB cursor semantics: it means "no limit" and may return all matching documents. Use a positive limit for bounded reads; explicit positive limits are capped by findMaxLimit.

📝 Usage example

//Basic usage - limit and skip
const results = await collection('products')
  .find({ category: 'electronics' })
  .limit(10)
  .skip(5);

//sort query
const results = await collection('products')
  .find({ inStock: true })
  .sort({ price: -1 })
  .limit(10);

//Field projection
const results = await collection('products')
  .find({ category: 'books' })
  .project({ name: 1, price: 1 })
  .limit(5);

//Complex combinations - multiple chained methods
const results = await collection('products')
  .find({ category: 'electronics', inStock: true })
  .sort({ rating: -1, sales: -1 })
  .skip(5)
  .limit(10)
  .project({ name: 1, price: 1 })
  .hint({ category: 1, price: -1 })
  .maxTimeMS(5000)
  .comment('Complex query');

//Query execution plan
const plan = await collection('products')
  .find({ category: 'electronics' })
  .sort({ price: -1 })
  .limit(10)
  .explain('executionStats');

//Streaming query
const stream = collection('products')
  .find({ category: 'books' })
  .sort({ createdAt: -1 })
  .limit(100)
  .stream();

2. aggregate() method

✅ Supported chain calls (9 methods in total)

MethodsSyntaxDescriptionExposed capabilities
.hint(spec).hint(object|string)Index promptaggregate() query chain
.collation(spec).collation(object)Sorting rulesaggregate() query chain
.comment(str).comment(string)Query commentsaggregate() query chain
.maxTimeMS(ms).maxTimeMS(number)Query timeoutaggregate() query chain
.allowDiskUse(bool).allowDiskUse(boolean)Allow disk useaggregate() query chain
.batchSize(n).batchSize(number)batch sizeaggregate() query chain
.explain(v).explain(string?)Return the aggregate execution planaggregate() query chain
.stream().stream()Return streaming resultsaggregate() query chain
.toArray().toArray()Explicit conversion to arrayaggregate() query chain

📝 Usage example (2. aggregate() method)

//Basic aggregation
const results = await collection('orders')
  .aggregate([
    { $match: { status: 'paid' } },
    { $group: { _id: '$category', total: { $sum: '$amount' } } }
  ])
  .allowDiskUse(true);

//Complete chain call
const results = await collection('orders')
  .aggregate([
    { $match: { status: 'paid' } },
    { $group: { _id: '$category', total: { $sum: '$amount' } } },
    { $sort: { total: -1 } }
  ])
  .hint({ status: 1, createdAt: -1 })
  .allowDiskUse(true)
  .maxTimeMS(10000)
  .comment('Category Sales Statistics');

//aggregate execution plan
const plan = await collection('orders')
  .aggregate([
    { $match: { status: 'paid' } },
    { $group: { _id: '$customerId', total: { $sum: '$amount' } } }
  ])
  .explain('executionStats');

//streaming aggregation
const stream = collection('orders')
  .aggregate([
    { $match: { status: 'paid' } },
    { $limit: 100 }
  ])
  .stream();

🆚 MongoDB native chain method comparison

Complete comparison table

MethodMongoDB native supportmonSQLize v3Description
.limit()Fully supported
.skip()Fully supported
.sort()Fully supported
.project()Fully supported
.hint()Fully supported
.collation()Fully supported
.comment()Fully supported
.maxTimeMS()Fully supported
.batchSize()Fully supported
.explain()Fully supported
.toArray()Fully supported
.stream()Fully supported (use .stream() instead of .forEach())
.forEach()Implemented via .stream()Use streaming instead
.map()Implemented via .stream()Use streaming instead
.hasNext()Not supported (conflicts with cache architecture)
.next()Not supported (conflicts with cache architecture)
.close()Not required (automatic management)

Summary: monSQLize v3 supports the 12 chaining methods listed above. Use the table as the capability contract; application coverage depends on the query patterns your service uses.


✨ Highlights of new features

1. Promise compatibility

The chained call object implements the complete Promise interface:

//directly await
const results = await collection('products').find({}).limit(10);

//Use .then()
collection('products')
  .find({}).limit(10)
  .then(results => console.log(results));

//Use .catch()
const results = await collection('products')
  .find({}).limit(10)
  .catch(err => []);

2. Automatic parameter verification

//✅ Correct
.limit(10)
.skip(5)

//❌ Error - automatically throw exception
.limit(-1)        // Error: limit() requires a non-negative integer
.skip("invalid")  // Error: skip() requires a non-negative integer
.sort("invalid")  // Error: sort() requires an object or array

3. Execution protection

To prevent accidental re-execution:

const chain = collection('products').find({}).limit(5);

//First time execution ✅
await chain.toArray();

//The second execution ❌ throws an error
await chain.toArray(); // Error: Query already executed

4. Full cache support

The chained call uses the same cache key as the options parameter:

//These two methods share the cache
await collection('products').find({}).limit(10).sort({ price: -1 });
await collection('products').find({}, { limit: 10, sort: { price: -1 } });

🔄 Backwards Compatibility

100% backwards compatible

All existing code without modification:

//Old Code - Keep working ✅
const results = await collection('products').find(
  { category: 'electronics' },
  { limit: 10, sort: { price: -1 } }
);

//New Code - Chained Calls ✅
const results = await collection('products')
  .find({ category: 'electronics' })
  .limit(10)
  .sort({ price: -1 });

Automatic detection

monSQLize will automatically detect the calling method:

  • No options parameter → return chain builder
  • with options parameter → execute query directly

Feedback and Suggestions: If you have questions or suggestions, please submit GitHub Issue.