What does the 1 in the projection of specific fields indicate in MongoDB?

QuestionsQuestions8 SkillsProInsert Data in MongoDBNov, 04 2025
082

In MongoDB, the 1 in the projection indicates that the field should be included in the result set. When you specify a field with a value of 1, it means you want that field to be returned in the query results. Conversely, if you specify a field with a value of 0, it means that field should be excluded from the results.

For example, if you have a collection and you want to include only the name and age fields in your query results, you would write:

db.collection.find({}, { name: 1, age: 1 })

This will return documents that include only the name and age fields, while excluding all other fields.

0 Comments

no data
Be the first to share your comment!