The find() method of a MongoDB collection queries the documents in that collection and returns a cursor containing the queried documents. The cursor can be assigned to a variable and iterated through to retrieve the documents. In mongo shell if the returned cursor is not assigned to a variable, it will be automatically iterated up to 20 times and prints the documents.
You can pass two parameters in JSON style document format to the find() method. The syntax is as below
db.CollectionName.find({search_criteria},{field_selectors})
The search_criteria specifies the conditions used to select the documents from the collection. If this parameter is omitted or an empty document is specified, then all documents in the collection are returned. The search criteria contains query operators that perform comparisons, logical operations etc., The table below shows some examples of search criteria documents.
search_criteria | Description |
---|---|
{ } | An empty search criteria document means return all documents in the collection. |
{ field1: value1 } | Returns all documents where field1 equals value1 |
{ field1: value1, field2: value2 } | Returns all documents where field1 equals value1 and field2 equals value2 |
{ field1: { $gt: value1 } } | Returns all documents where field1 greater than value1 |
{ field1: { $gt: value1, $lt: value2 } } | Returns all documents where field1 greater than value1 and less than value2 |
{ $or: [ { field1: value1 }, { field2: { $ne: value2 } ] } | Returns all documents where either field1 equal value1 or field2 not equal to value2 |
In the above example $gt, $lt, $ne, $or are query operators.
The field selector is an optional parameter that can be used to limit the fields returned for the documents. If this parameter is omitted then all fields in the document are returned. A field selector document has the form
{ field1: boolean_value, field2: boolean_value ... }
The boolean_value for a field can be either 0 or 1. A value 1 for a field tells the find() method to include only those fields plus the _id field. A value 0 for a field means to exclude those fields.
A field selector document can either contain a set of fields to include or a set of fields to exclude. You cannot have both include and exclude specifications together. However if you have a field selector document that specifies the fields to include, you can explicitly omit the _id field.
To query all movies from the movie collection that were released in the year 1994
> db.movie.find( {releaseYear: 1994 } ) { "_id" : ObjectId("53623324995b194b63078522"), "title" : "Pulp Fi ction", "releaseYear" : 1994, "language" : "English", "director" : "Quentin Tarantino" } >
To query all movie titles and their release year
> db.movie.find( { }, {title: 1, releaseYear: 1} ) { "_id" : ObjectId("53623275995b194b63078520"), "title" : "Gravit y", "releaseYear" : 2013 } { "_id" : ObjectId("53623324995b194b63078521"), "title" : "The bo ok thief", "releaseYear" : 2013 } { "_id" : ObjectId("53623324995b194b63078522"), "title" : "Pulp F iction", "releaseYear" : 1994 } { "_id" : ObjectId("53623324995b194b63078523"), "title" : "Django Unchained", "releaseYear" : 2013 } { "_id" : ObjectId("53623324995b194b63078524"), "title" : "The Ma trix", "releaseYear" : 1999 } >
In the above example you will notice that _id field is included even though it is not explicitly specified in the field selector document. If you want to omit the _id field from the above output, run find method as below
> db.movie.find( { }, {title: 1, releaseYear: 1, _id:0} ) { "title" : "Gravity", "releaseYear" : 2013 } { "title" : "The book thief", "releaseYear" : 2013 } { "title" : "Pulp Fiction", "releaseYear" : 1994 } { "title" : "Django Unchained", "releaseYear" : 2013 } { "title" : "The Matrix", "releaseYear" : 1999 } >
To query the title of all movies directed by Quentin Tarantino and released after year 2000
> db.movie.find( {director: "Quentin Tarantino", releaseYear: {$gt: 2000}}, {title: 1, _id:0} ) { "title" : "Django Unchained" } >
To query the title of all movies starring Sandra Bullock or Emily Watson
> db.movie.find( { $or: [{cast: "Sandra Bullock"}, {cast: "Emily Watson"}]}, {title: 1, _id:0} ) { "title" : "Gravity" } { "title" : "The book thief" } >
The findOne() method is a variant of find() that returns only one document that matches the search criteria. If multiple documents match the search criteria then only the first document, in the order in which they are stored in the disk, is returned. Unlike find()which returns a cursor, findOne() returns a single document. The syntax of findOne() method is as below.
db.CollectionName.findOne({search_criteria},{field_selectors})
The arguments search_criteria and field_selectors are the same as for find
and are optional.
> db.movie.findOne() { "_id" : ObjectId("53623275995b194b63078520"), "title" : "Gravity", "releaseYear" : 2013, "language" : "English", "director" : "Alfonso Cuarón", "cast" : [ "Sandra Bullock", "George Clooney", "Ed Harris", "Paul Sharma" ] } >
The results of find() are by default displayed in a condensed format that is not very pleasant for human reading. You can use the pretty() method to display the find() results in a easy to read format.
> db.movie.find( {releaseYear: 1994 } )
{ "_id" : ObjectId("57dbbe4d676b10bfb34a55a4"), "title" : "Pulp
Fiction", "releaseYear" : 1994, "language" : "English", "direct
or" : "Quentin Tarantino" }
> db.movie.find( {releaseYear: 1994 } ).pretty()
{
"_id" : ObjectId("57dbbe4d676b10bfb34a55a4"),
"title" : "Pulp Fiction",
"releaseYear" : 1994,
"language" : "English",
"director" : "Quentin Tarantino"
}
>
Nothing yet..be the first to share wisdom.