Introduction
Projection helps you get only the data you need from a database, making data transfer faster and lighter.
db.collection.find(query, { field1: 1, field2: 1, _id: 0 })db.users.find({}, { name: 1, email: 1, _id: 0 })db.products.find({ category: 'books' }, { title: 1, price: 1 })db.orders.find({}, { _id: 0, orderDate: 1 })db.employees.insertMany([
{ name: 'Alice', age: 30, department: 'HR' },
{ name: 'Bob', age: 25, department: 'Sales' },
{ name: 'Carol', age: 27, department: 'HR' }
])
// Find all employees but only show their name and department, exclude _id
db.employees.find({}, { name: 1, department: 1, _id: 0 })projection in a MongoDB query?name and age fields in a MongoDB find query?{"_id": 1, "name": "Alice", "age": 25, "city": "NY"}{"_id": 2, "name": "Bob", "age": 30, "city": "LA"}db.collection.find({}, {name: 1, _id: 0})name with 1 and excludes _id with 0, so only the name field appears without _id.name field, no _id or other fields.password field:db.users.find({}, {password: 1, email: 0})password (1) and exclude email (0) together, which is invalid.title and author fields from a large books collection but exclude the _id field to reduce data size. Which query correctly achieves this and why?title and author fields and exclude _id to reduce data size._id uses 0. Mixing inclusion and exclusion (except _id) is not allowed.title and author with 1 and excludes _id with 0. Others either exclude needed fields or mix inclusion/exclusion incorrectly.Pass the test and this pattern moves unknown