Error
Error Code:
47
MongoDB Error 47: No Document Found
Description
MongoDB Error 47, 'No Matching Document', indicates that a database operation (such as findOne, updateOne, or deleteOne) could not locate any document that satisfies the specified query filter. This typically occurs when the query criteria do not match any existing documents in the target collection.
Error Message
No Matching Document
Known Causes
4 known causesIncorrect Query Filter
The query filter provided to the operation does not accurately reflect the criteria of any existing document in the collection.
Data Type Mismatch
The data types of values in your query filter do not match the actual data types stored in the documents, preventing a match.
Document Does Not Exist
The document you are attempting to find or modify has either been deleted or was never created in the specified collection.
Case Sensitivity Issues
For string fields, the query might be case-sensitive, and the casing in the query does not match the casing in the stored document.
Solutions
4 solutions available1. Verify Query Criteria easy
Double-check that the query's filter, projection, and sort criteria accurately reflect the data you expect to find.
1
Review the query being executed. Pay close attention to the `filter` (the `WHERE` clause equivalent) to ensure all conditions are correct.
db.collection.find({ field1: 'expected_value', field2: 123 })
2
Verify the `projection` to ensure you are not inadvertently excluding the fields that would otherwise allow the document to match.
db.collection.find({ _id: 'some_id' }, { _id: 1, relevant_field: 1 })
3
If `sort` is used, ensure it's not making a document appear to not exist due to sorting order in conjunction with other query logic (less common for Error 47 directly, but can lead to unexpected results).
db.collection.find({ status: 'active' }).sort({ timestamp: -1 })
4
Execute a simpler query to confirm the existence of documents that *should* match. For example, query by a unique identifier like `_id` if available.
db.collection.findOne({ _id: 'a_known_document_id' })
2. Inspect Data for Typos and Case Sensitivity easy
Ensure that the values used in your query criteria exactly match the data in your collection, paying attention to case and spelling.
1
Manually inspect the data in your collection for the fields you are querying. Use `findOne` or `find` with a broad filter to sample existing documents.
db.collection.find({}, { field_to_check: 1 }).limit(5)
2
Compare the values in your query string literally against the values retrieved from the database. MongoDB string comparisons are case-sensitive by default.
// Query:
db.collection.find({ name: 'John Doe' })
// Data in DB might be:
{ name: 'john doe' } // This will not match the query above
3
If case-insensitivity is required, use regular expressions with the `i` option.
db.collection.find({ name: { $regex: '^john doe$', $options: 'i' } })
3. Check for Indexing Issues medium
While not a direct cause of 'No Matching Document', incorrect or missing indexes can lead to inefficient queries or unexpected behavior that might be misinterpreted.
1
List existing indexes for the collection to understand how queries are being optimized.
db.collection.getIndexes()
2
If your query uses multiple fields in the filter, ensure there's a compound index that matches the order of fields in your query for optimal performance.
// Example Query:
db.collection.find({ status: 'active', category: 'electronics' })
// Potentially useful index:
db.collection.createIndex({ status: 1, category: 1 })
3
If you suspect an index might be causing issues (e.g., a unique index preventing expected inserts/updates), consider dropping and recreating it after careful consideration. **Use with caution.**
db.collection.dropIndex('index_name')
4
Use `explain()` on your query to see if indexes are being used and how the query is being executed. This can reveal if the query is performing a collection scan when an index should be used.
db.collection.find({ field: 'value' }).explain('executionStats')
4. Confirm Correct Database and Collection easy
Ensure your application or client is connected to the correct MongoDB instance, database, and collection where the data is expected to reside.
1
Verify the connection string or the active database context in your MongoDB client or application code.
// Example in Node.js driver:
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
await client.connect();
const db = client.db('your_database_name');
const collection = db.collection('your_collection_name');
2
In the `mongo` shell, check the current database using `db.getName()` and switch if necessary using `use database_name`.
db.getName()
use other_database_name
3
List all databases and collections to confirm their existence and names.
show dbs
show collections