Why use a MongoDB query generator?
MongoDB's syntax is powerful but not always intuitive. Remembering the exact structure of $lookup, $facet, or $geoNear takes time. This generator gives you real examples you can adapt: change collection names, adjust fields, and you're done. It's especially useful when migrating from SQL and still thinking in JOINs instead of pipelines, or when you need to prototype quickly without consulting docs. Examples cover everything from basic CRUD to multi-stage aggregations and geospatial operations.
Common mistakes when writing MongoDB queries
Forgetting ObjectId(): If you search by _id with a string, it won't match; you need ObjectId('...'). Confusing $set with $push: $set replaces a field; $push appends to an array. Using $size with ranges: $size only accepts exact values; for ranges use $expr with $gte. Not indexing filtered fields: Queries on unindexed fields are slow on large collections; create indexes with createIndex. Pipelines without early $match: Put $match as early as possible to reduce processed documents.
Operators every MongoDB developer should know
$match filters documents (equivalent to WHERE). $group aggregates and calculates (SUM, AVG, COUNT). $lookup joins collections. $project selects/transforms fields. $unwind deconstructs arrays into individual documents. $sort orders (1 ascending, -1 descending). $limit and $skip paginate results. $addFields creates calculated fields without removing existing ones. $facet runs multiple pipelines in parallel. $geoNear searches by geographic proximity. Combine these operators for complex queries while maintaining readability.
When to use aggregate instead of find
Use find() for simple queries: filter, sort, limit. It's faster and more direct when you don't need to transform data. Switch to aggregate() when you need to: calculate totals/averages by group, join with $lookup, flatten arrays with $unwind, create calculated fields, filter after grouping ($match post-$group), or run complex analytics. Aggregate is more powerful but also heavier; if you only need { status: 'active' }, find is enough. Rule of thumb: if you need more than one transformation step, aggregate is your tool.