Skip to main content

Server-side processing for DataTables and Editor with MongoDB

Project description

mongo-datatables

PyPI version Downloads License: MIT codecov Tests

Server-side processing for jQuery DataTables with MongoDB.

Overview

This package provides an elegant bridge between jQuery DataTables and MongoDB databases, handling the translation of DataTables server-side requests into optimized MongoDB queries. It supports both read operations and full CRUD functionality when paired with DataTables Editor.

Key Capabilities

  • Server-side processing for efficient handling of large datasets
  • Advanced search functionality with column-specific filtering
  • Multi-column sorting with MongoDB optimization
  • Complete Editor integration for create, read, update, and delete operations
  • Framework-agnostic design compatible with Flask, Django, and other Python web frameworks

Installation

pip install mongo-datatables

Basic Implementation

from flask import request, jsonify
from mongo_datatables import DataTables

@app.route('/data/<collection>', methods=['POST'])
def get_data(collection):
    data = request.get_json()
    results = DataTables(mongo, collection, data).get_rows()
    return jsonify(results)

Documentation

For comprehensive documentation, visit mongo-datatables.readthedocs.io

Search Functionality

How Search Works in mongo-datatables

mongo-datatables provides powerful search capabilities that adapt based on your MongoDB configuration and search syntax. Understanding how search works can help you optimize performance, especially for large collections.

Search Types and Performance

Type Example Large Collection Perf Description
Text George Orwell Fast 100-300ms Text search with indexes (OR semantics)
Phrase "Margaret Atwood" Fast 100-300ms Exact phrase matching (exact match)
Field Author:Bradbury Moderate 1-2s Field-specific search (single field)
Comparison Pages:>100 Fast 200-500ms Numeric/date compare (>, <, >=, <=, =)
Combined Author:"Huxley" Year:>2000 Moderate 0.5-1s Multiple search types (AND semantics)
Regex George Orwell (no index) Slow 5-10s+ Fallback search (OR semantics)
Mixed Title:"Ski" Ishiguro Moderate 300-700ms Phrase + text (AND between terms)

Performance metrics based on large collections (>2M docs)

Performance Optimization

Importance of Indexes for Large Collections

When working with large MongoDB collections, creating proper indexes is critical for performance. Without appropriate indexes, queries can become extremely slow or timeout entirely.

Text Indexes for Search Performance

The DataTables class is designed to leverage MongoDB text indexes for efficient search operations:

# Create a text index in MongoDB (do this once in your setup script)
db.your_collection.create_index([("field1", "text"), ("field2", "text")])

# DataTables will automatically use the text index when available
datatables = DataTables(mongo, 'your_collection', request_args)

Benefits of text indexes:

  • Dramatically faster search on large collections
  • Better relevance scoring for search results
  • Language-aware stemming for more natural search
  • Support for exact phrase queries using quotes

If a text index is not available, the library will fall back to regex-based search, which is significantly slower for large collections.

Regular Indexes for Sorting and Filtering

In addition to text indexes, create regular indexes for fields used in sorting and filtering:

# Create indexes for commonly sorted/filtered fields
db.your_collection.create_index("created_at")
db.your_collection.create_index("status")

Note: MongoDB has a limit of one text index per collection, but you can include multiple fields in a single text index.

Development

Testing

Run the tests:

python run_tests.py

Generate coverage report:

python run_coverage.py

Coverage reports are available in the htmlcov directory.

License

Released under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mongo_datatables-1.1.2.tar.gz (50.1 kB view details)

Uploaded Source

Built Distribution

mongo_datatables-1.1.2-py3-none-any.whl (48.9 kB view details)

Uploaded Python 3

File details

Details for the file mongo_datatables-1.1.2.tar.gz.

File metadata

  • Download URL: mongo_datatables-1.1.2.tar.gz
  • Upload date:
  • Size: 50.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mongo_datatables-1.1.2.tar.gz
Algorithm Hash digest
SHA256 20f350fd014e3bb2bf07ae8f685879180c79f59d66669c606fbf20162dbeea4c
MD5 756d7e18628b1bc707c51e301732fbe3
BLAKE2b-256 7a15d2fa9093940f13d4b30a71a235c02f9a0b80c751e1c657aff148b031f497

See more details on using hashes here.

File details

Details for the file mongo_datatables-1.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for mongo_datatables-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 55381315176b731fb14e4b9afefc392cbdf847a9a552b625e8c214bc91d3c05a
MD5 0d3aac460f0aa0561f33494b3218e0eb
BLAKE2b-256 01e20c6f5c081468ee9c11965861347a5906227a1eafa778a444cd7978c0a368

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page