Find
Estimated time to read: 1 minute
The find
command can be used to query a MongoDB database via a shell connection.
Default Databases¶
admin
and local
are both default databases that are used for administrative actions and storage.
Find command flow¶
-
show dbs
shows the list of databases that are within the cluster. -
use <database_name>
selects the database to be used for any following commands. -
show collections
shows the collections within the database. -
db.<collection_name>.find(<json_query>)
actions the find command against the specified collection, searches for the json query that has been passed in and returns the results.
it
Iterate¶
The command it
is used to iterate through the cursor. A cursor is a pointer to the result set of a query. A pointer is a direct address to a memory location.
Additional Operations¶
When using find
additional operations can be appended to return results based upon the data in the data base.
For example:
-
db.<collection_name>.find(<json_query>).count()
returns the number of documents that match the specified query. -
db.<collection_name>.find(<json_query>).pretty()
returns prettified JSON, that is much more suitable for human readability.
If the find
command is run without a query, db.<collection_name>.find()
, a random selection of 20 documents are returned from the collection.