Error
Error Code: 97

MongoDB Error 97: Missing or Invalid Projection

📦 MongoDB
📋

Description

MongoDB Error 97, 'No Projection Found', indicates that a database operation requiring a projection document was executed without a valid projection specified. This typically occurs when a query attempts to limit returned fields but provides an empty, malformed, or missing projection object.
💬

Error Message

No Projection Found
🔍

Known Causes

3 known causes
⚠️
Empty Projection Object
A query operation was provided with an empty `{}` object in the projection argument, which is not considered a valid projection to specify fields.
⚠️
Malformed Projection Syntax
The projection argument provided is not a valid BSON document, such as a string or an array, or contains syntax errors that prevent its interpretation as a projection.
⚠️
Projection in Unsupported Context
A projection was specified in a database command or method that does not support projection arguments, or expects them in a different format than provided.
🛠️

Solutions

3 solutions available

1. Add a Projection to Your Query easy

Explicitly define which fields to return when querying documents.

1
When performing a `find()` operation, include a second argument that specifies the fields you want to include (1) or exclude (0). If you want to include all fields, you can explicitly specify them, or in most cases, omitting the projection will return all fields by default. However, the error indicates a missing or invalid projection in a context where one is expected.
db.collection.find(<query>, { <field1>: 1, <field2>: 1, ... });
2
If you intend to return all fields, ensure the projection is correctly formatted, even if it's an empty object or a document with all fields set to 1. However, the most common cause of Error 97 is when a projection is *required* by the operation or driver, and it's missing.
db.collection.find(<query>, {}); // This might still cause issues if a projection is mandatory.
3
The most reliable way to include all fields when a projection is needed is to explicitly list them or use a common pattern if known.
db.collection.find(<query>, { _id: 1, field1: 1, field2: 1 });

2. Review Driver or API Usage for Projection Requirements medium

Check the documentation for your MongoDB driver or API to understand where projections are mandatory.

1
Certain MongoDB operations or specific driver methods might implicitly require a projection, even if a standard `find()` operation doesn't. For example, some aggregation pipeline stages might expect a projection.
// Example: In some aggregation contexts, the $project stage is explicit.
2
Consult the documentation for the specific MongoDB driver (e.g., Node.js, Python, Java) or API you are using. Look for sections related to query construction, field selection, or projection. Identify any methods or operations that mandate a projection argument.
/* Consult your driver's documentation for methods like find(), aggregate(), etc. */
3
Ensure that when you call these methods, you are providing a valid projection object, even if it's an empty object `{}` or a document that includes all fields with a value of `1`.
/* For example, in a Node.js driver, if a method signature requires a projection, pass it: */
db.collection.find(query, { projection: { field1: 1 } });

3. Inspect Aggregation Pipeline for Missing $project Stage medium

Ensure that if an aggregation pipeline is used, a $project stage is correctly defined when needed.

1
If you are encountering this error within an aggregation pipeline, it's highly probable that a `$project` stage is either missing when expected or is malformed. The `$project` stage is used to reshape documents, including selecting, renaming, or excluding fields.
db.collection.aggregate([
  { $match: { ... } },
  { $project: { field1: 1, field2: 1, _id: 0 } } // Example $project stage
]);
2
Review each stage of your aggregation pipeline. If a stage requires specific fields to be present or formatted in a certain way, and the preceding stages don't provide them, you might need to add a `$project` stage earlier in the pipeline to shape the documents appropriately.
/* Example: If a subsequent stage expects 'newFieldName' but the previous stages don't create it */
db.collection.aggregate([
  { $match: { ... } },
  { $addFields: { tempField: '$oldField' } },
  { $project: { newFieldName: '$tempField', _id: 0 } } // Projecting the renamed field
]);
3
Alternatively, if the error arises because a projection is expected at the *end* of the pipeline for the final output, ensure your last stage (or a dedicated `$project` stage) correctly defines the output structure.
db.collection.aggregate([
  { $match: { ... } },
  { $group: { ... } },
  { $project: { finalField1: 1, finalField2: 1 } } // Final projection for output
]);
🔗

Related Errors

5 related errors