In this article, we focus on the backend for mobile app on MongoDB Atlas, covering indexes and search. When developing a mobile catalog with 200,000 products, we encountered 3-second search delays. After implementing proper indexes and Atlas Search, response time dropped to 50 ms — a 60x improvement. This article covers setting up an Atlas cluster with a replica set, designing schemas with Mongoose, building indexes for mobile queries, implementing search via Atlas Search, and real-time updates with Change Streams. Based on real experience — over 30 mobile projects (iOS, Android, Flutter) in 5 years. MongoDB for mobile apps often requires a special approach, and we'll show how to avoid common pitfalls. Directly connecting to MongoDB from a mobile client via a native driver is bad practice: credentials in code, no authorization layer. Instead, we use Atlas, API Gateway, and Device Sync. Average API response time with properly configured indexes is 10–50 ms, which is 40–200x faster than without indexes. According to MongoDB documentation, proper indexing can reduce query time by over 90%. Cost savings on operations can reach 80%, and a typical Atlas cluster for a mobile app costs between $500 and $2,000 per month depending on scale. In one project, we reduced monthly Atlas costs from $2,500 to $500, saving $2,000 per month. Development time is reduced by 30% thanks to Atlas's ready-made solutions.
Two Usage Strategies
Choosing Between Backend API and Device Sync
Strategy 1: MongoDB as backend database. The mobile client calls an API (Node.js + Mongoose), the API works with MongoDB. Standard server architecture.
Strategy 2: Atlas Device SDK (Realm) + Atlas Device Sync. A local Realm database on the device automatically syncs with Atlas MongoDB in the cloud. Two-way synchronization without writing sync logic manually.
For most projects, the first strategy is suitable. The second is justified when complex offline requirements exist: the app must work without internet and automatically sync after reconnection. If data is not critical offline or you need custom business logic on the server, REST API with Mongoose is simpler and more flexible.
Using Atlas Data API
For simple CRUD operations without a custom backend, Atlas offers Data API — HTTP endpoints to MongoDB via HTTPS with authentication using API keys or JWT. Suitable for prototypes and simple apps. In production with custom business logic, Data API is not flexible enough — use a full-fledged backend.
API with Mongoose on Node.js
// Schema with validation
const productSchema = new Schema({
_id: { type: String, default: () => new ObjectId().toHexString() },
title: { type: String, required: true, maxlength: 200 },
categoryId: { type: String, required: true, index: true },
priceCents: { type: Number, required: true, min: 0 },
images: [{ url: String, width: Number, height: Number }],
tags: [{ type: String, index: true }],
metadata: Schema.Types.Mixed,
isActive: { type: Boolean, default: true, index: true },
createdAt: { type: Date, default: Date.now, index: true }
}, {
collection: 'products',
versionKey: false
})
// Compound index for typical mobile queries
productSchema.index({ categoryId: 1, isActive: 1, createdAt: -1 })
productSchema.index({ tags: 1, isActive: 1 })
The most common mistake is forgetting index: true on fields used for filtering. Without an index, MongoDB does a collection scan — on 100K documents that's seconds instead of milliseconds. Our tests showed that a compound index on categoryId, isActive, createdAt speeds up catalog queries by 40x.
Aggregation Pipeline for Mobile API
find() with simple filters is for straightforward queries. For statistics, complex selections, or joining collections, use the aggregation pipeline:
// Catalog with product count per category
const categoriesWithCount = await Category.aggregate([
{ $match: { isActive: true } },
{
$lookup: {
from: 'products',
let: { categoryId: '$_id' },
pipeline: [
{ $match: { $expr: { $and: [
{ $eq: ['$categoryId', '$$categoryId'] },
{ $eq: ['$isActive', true] }
]}}},
{ $count: 'total' }
],
as: 'productCount'
}
},
{
$project: {
name: 1,
slug: 1,
imageUrl: 1,
productCount: { $ifNull: [{ $first: '$productCount.total' }, 0] }
}
},
{ $sort: { sortOrder: 1 } }
])
Atlas Search for Full-Text Search
Full-text search via Atlas Search (Lucene under the hood) is faster than regex or a text index. Fuzzy search with maxEdits:1 finds "ноутбук" when the user types "ноутьбук" — critical for mobile search due to typos on virtual keyboards.
// Atlas Search index definition (in Atlas Console or via Atlas API)
// {
// "mappings": {
// "fields": {
// "title": [{ "type": "string", "analyzer": "lucene.russian" }],
// "description": [{ "type": "string", "analyzer": "lucene.russian" }]
// }
// }
// }
const searchResults = await Product.aggregate([
{
$search: {
index: 'product_search',
compound: {
must: [{
text: {
query: searchQuery,
path: ['title', 'description'],
fuzzy: { maxEdits: 1 }
}
}],
filter: [{ equals: { path: 'isActive', value: true } }]
}
}
},
{ $limit: 20 },
{ $project: { title: 1, priceCents: 1, thumbnailUrl: 1, score: { $meta: 'searchScore' } } }
])
According to MongoDB Atlas documentation, an index with the lucene.russian analyzer correctly handles stemming for the Russian language, improving search relevance.
Change Streams for Real-Time Updates
MongoDB Change Streams are similar to PostgreSQL LISTEN/NOTIFY. The backend subscribes to collection changes and broadcasts events to mobile clients via WebSocket:
const changeStream = Product.watch([
{ $match: { operationType: { $in: ['insert', 'update', 'delete'] } } }
], { fullDocument: 'updateLookup' })
changeStream.on('change', (change) => {
if (change.operationType === 'update') {
const updatedProduct = change.fullDocument
wsServer.broadcast(`category:${updatedProduct.categoryId}`, {
type: 'PRODUCT_UPDATED',
data: updatedProduct
})
}
})
Change Streams require a MongoDB replica set — in Atlas this is enabled by default. An alternative is Atlas Device Sync, which tracks changes and syncs them to clients automatically.
Response time comparison with and without indexes
| Query type | Without index | With index | Speedup |
|---|---|---|---|
| Search by categoryId | 2.1 s | 12 ms | ×175 |
| Search by tags | 1.8 s | 8 ms | ×225 |
| Aggregation with $lookup | 3.4 s | 45 ms | ×75 |
Process of Work
- Requirements analysis — determine load profile, data volume, offline requirements.
- Schema design — model documents for mobile queries, plan indexes.
- Atlas setup — replication, backups, clustering.
- API implementation — Mongoose schemas, aggregations, Atlas Search, Change Streams.
- Mobile client integration — network layer, caching, error handling.
- Deployment and monitoring — Atlas monitoring, alerts, load testing.
What's Included
| Stage | Result |
|---|---|
| Analysis | Architectural decision document |
| Design | ER diagram, schema specification |
| Atlas setup | Instance + replication + backups |
| Development | API + client SDK |
| Testing | Performance report |
| Training | Documentation and 2-week support |
Why Choose Us
Our team has 5+ years of experience with MongoDB Atlas, with over 30 successful mobile projects (iOS, Android, Flutter). We guarantee 99.9% SLA for the Atlas cluster and provide query optimization consultations. Configuring MongoDB Atlas, schemas, indexes, Atlas Search, and API for a mobile app takes 1 to 3 weeks. Cost is calculated individually. Get a consultation from a MongoDB engineer who has worked with App Store Review Guidelines and Google Play Console.
Contact us for a consultation on configuring MongoDB Atlas for your mobile app. Reach out for a project assessment.







