Mongo中Empty object转query string:二者是否相同?会引发性能问题吗?
Great question—let’s break this down clearly, since this comes up more often than you might think!
Equivalence: Empty Object {} vs. Its Query String Form
Short answer: Yes, they are completely equivalent in MongoDB.
When you pass an empty object {} as a query (e.g., db.collection.find({})), or if you convert that empty object to a query string (like ?filter={} in an API request, which your driver will parse back into an empty object), MongoDB treats them exactly the same way. Both tell the database to match every document in the collection. There’s no semantic difference between the two representations—they resolve to the same query logic under the hood.
Performance Impact of Empty Object Queries
Now, about speed: The empty object itself doesn’t cause performance issues directly. The problem comes from what the query does, not how it’s represented.
- An empty object query asks MongoDB to return all documents in the collection. If your collection is large (think millions of docs) and you don’t have an index that can cover the query (like the default
_idindex), this will trigger a full collection scan. Full scans are slow because they have to read every document on disk, which is far less efficient than using an index. - If you do have an index (even the default
_idindex), MongoDB might use it to iterate through all documents, but it still has to process every entry in the index and fetch the corresponding documents (unless you’re doing a covered query with projection). - The act of converting an empty object to a query string and back adds negligible overhead—drivers handle this parsing quickly, so it’s not a bottleneck compared to the actual query execution.
In short: The performance hit (if any) comes from querying the entire collection, not from using an empty object or converting it to a query string.
内容的提问来源于stack exchange,提问作者Michalis

