Convert a Simple SQL SELECT to MongoDB find()

Learn how to convert a basic SQL SELECT statement with column selection into a MongoDB find() query with projection. Covers field mapping basics.

Basic Queries

Detailed Explanation

From SELECT to find()

The most fundamental conversion maps a SQL SELECT statement to MongoDB's db.collection.find() method. The first argument is the filter (empty {} for no conditions), and the second argument is the projection that controls which fields are returned.

Example SQL

SELECT name, email, age
FROM users

Generated MongoDB Query

db.users.find(
  {},
  {
    name: 1,
    email: 1,
    age: 1,
    _id: 0
  }
)

Key Mapping Rules

SQL Element MongoDB Equivalent
SELECT col1, col2 Projection object with fields set to 1
SELECT * No projection argument (all fields returned)
FROM table db.table collection reference
Column exclusion _id: 0 to suppress the default _id field

Projection Behavior

In MongoDB, a projection document uses 1 to include fields and 0 to exclude them. You cannot mix inclusion and exclusion in the same projection, with the sole exception of _id, which can always be explicitly excluded. When no projection is provided, all fields in the document are returned, equivalent to SELECT * in SQL.

Collection Naming

SQL table names map directly to MongoDB collection names. While SQL tables often use plural snake_case (user_accounts), MongoDB collections follow the same convention by default. The converter preserves the original table name as the collection name.

Use Case

When migrating a reporting dashboard from a relational database to MongoDB, you often start by converting the simplest read queries. This pattern establishes the baseline for all other conversions and helps developers understand how MongoDB's find() method corresponds to SQL SELECT.

Try It — SQL to MongoDB Query

Open full tool