Database Reference
In-Depth Information
There are three parts to the find( ) function:
Database . We need to identify which database we are querying. More accurately,
the variable pointing to the database we are querying, such as in this case db .
Collection . We need to identify the collection within this database we are query-
ing.
Parameters . We need to identify the documents and fields within this collection
we would like displayed. The first parameter contains the criteria for displaying the
documents, including optionally using operators to bring back one or more docu-
ments. The second parameter called “projection” specifies which fields to display.
In RDBMS terms, the first parameter specifies the rows and the second specifies
the columns. Note that if we leave out both parameters, all documents will be dis-
played (up to the first 20 and then more can be displayed by typing it) .
So if we would like to display all topics in our topic collection, we can execute:
db.book.find( )
If we would like to bring back a specific book in our collection, we can execute:
db.book.find( { titleName : “Data Modeling for MongoDB” } )
If we would like to display a specific book in our collection and only the titleName field
(along with _id ), we can execute:
db.book.find( { titleName : “Data Modeling for MongoDB” },
{ titleName : 1 } )
The 1 after a field name tells MongoDB we would like this field displayed. To exclude a
field, use a 0 . So if we would like all of the fields displayed except the titleName field, we
can execute:
db.book.find( { titleName : “Data Modeling for MongoDB” }, { titleName : 0 } )
If there is more than one field in the find clause, there is an implied “AND” between the
conditions. That is, all conditions must be true. So, for example, to find the topic titled Ex-
treme Scoping , which contains 300 pages, we can write this query:
Search WWH ::




Custom Search