MongoDB find() Builder
Build a MongoDB find() with filter, projection and sort.
MongoDB find in depth
db.collection.find(filter, projection) is the primary read operation in MongoDB. The first argument is a filter document matched against every record in the collection; the second is an optional projection selecting which fields come back. The call returns a cursor β a lazy stream that the driver materialises with .toArray(), .forEach(), .map() or .next(). MongoDB stores documents in BSON, a binary superset of JSON that adds types like ObjectId, Date, Decimal128 and Binary.
Filter operators
- Comparison:
$eq,$ne,$gt,$gte,$lt,$lte,$in [array],$nin. - Element:
$exists: true/false,$type: 'string'. - Evaluation:
$regex /pattern/i,$mod: [div, rem],$expr,$jsonSchema. - Logical:
$and,$or,$nor,$not. - Array:
$all,$elemMatch,$size. - Geospatial:
$near,$geoWithin,$geoIntersects(need a 2dsphere index).
Projection and cursor modifiers
Projection is inclusive ({ name: 1, email: 1 }) or exclusive ({ password: 0 }); the two modes cannot be mixed, except for _id, which is included by default and must be turned off with { _id: 0 }. Chained modifiers fine-tune the result set: .sort({ createdAt: -1 }), .limit(n), .skip(n), .hint({ field: 1 }) to force an index, and .countDocuments(filter) for an accurate count (the legacy .count() is deprecated). .explain('executionStats') reveals the winning plan, totalDocsExamined and executionTimeMillis β the single most useful debugging command in MongoDB.
Indexes and the ESR rule
A B-tree index is created with db.collection.createIndex({ field: 1 }). Compound indexes follow the ESR rule: place Equality fields first, then Sort fields, then Range fields. Specialised types include text (full-text), 2dsphere (geospatial), hashed (sharding), partial (filtered) and TTL (auto-expiration). When a query cannot use an index it triggers a COLLSCAN β a full collection scan β which is fine for development but catastrophic in production.
When to switch to aggregation
Once you need grouping, joins, derived fields or multi-stage transformations, leave find behind for the aggregation pipeline: $match, $project, $group, $sort, $lookup (left join), $unwind (explode arrays), $bucket (histograms) and $facet (parallel sub-pipelines). The first $match stage of a pipeline behaves like a find filter and is optimised the same way.
FAQ
Does $or use indexes? Yes, but only if every branch of the $or has an index on its field β otherwise MongoDB falls back to a collection scan. explain shows the OR stage and which children use which index.
Why is pagination with .skip(N).limit(20) slow on large collections? Skip still walks every previous document. Prefer range pagination: store the last _id or createdAt and query { _id: { $gt: lastId } }; this hits the index directly and runs in constant time.
How is MongoDB different from a SQL database? Each row becomes a self-contained JSON-like document; arrays and nested objects replace JOINs; the schema is flexible (no ALTER TABLE); and horizontal sharding is native via shard keys and config servers.
Is Mongoose required? No. Mongoose is an ODM that adds a schema layer, hooks and validators on top of the official mongodb driver. The driver alone is faster and lighter; Mongoose helps when you need declarative validation and consistent shapes.
Related Tools
Handwriting Generator
Convert typed text into an image with handwriting appearance. Useful for adding a personal touch to digital work.
Resume Generator
Fill a simple printable A4 CV from a form with personal data, education and experience.
Favicon Generator
Generate a favicon from text/emoji in all common sizes (16, 32, 48, 64, 192, 512). PNG download.