Most of the time, you use a library such as the native MongoDB driver or Mongoose to access documents in a collection. However, there may be times when you need to look at documents inside the MongoDB shell.
The MongoDB shell provides full querying functionality to find documents in collections, using the find(query)
method on the collection
object. The optional query
parameter specifies a query document with fields and values to match documents against in the collection. The documents that match the query are removed from the collection. Using the find()
method with no query
parameter returns all documents in the collection.
For example, the following lines of code first query every item in the collection and then retrieve the documents whose speed
field is equal to 120mph
(see Figure 12.9):
use testDB
coll = db.getCollection("newCollection")
coll.find()
coll.find({speed:"120mph"})