MongoDB - Query a Collection

MongoDB provides the db.collection.find() method to query documents within a collection.

The db.collection.find() selects documents in a collection and returns a cursor to the selected documents.

Return all Documents

This example returns all documents from the musicians collection:

Result:

{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }
{ "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 }

It returns all documents because we didn't pass any parameters as filtering criteria.

The above query is a shortened version of db.musicians.find( {} ). In the above query, we omitted the curly braces {}. This is perfectly valid when working with MongoDB.

Add Filtering Criteria

You can filter the results down by providing only the criteria that you're interested in.

For example, if we're only interested in Deep Purple from the artists collection:

Result:

{ "_id" : ObjectId("5781f85d48ef8c6b3ffb0150"), "artistname" : "Deep Purple", "albums" : [ { "album" : "Machine Head", "year" : 1972, "genre" : "Rock" }, { "album" : "Stormbringer", "year" : 1974, "genre" : "Rock" } ] }

Format the Results

You might find the above results a bit hard to read. The document is returned as one long line of text.

You can use the pretty() method to format the results so that they're a bit easier to read.

Just append pretty() to the end, like this:

Result:

{
	"_id" : ObjectId("5781f85d48ef8c6b3ffb0150"),
	"artistname" : "Deep Purple",
	"albums" : [
		{
			"album" : "Machine Head",
			"year" : 1972,
			"genre" : "Rock"
		},
		{
			"album" : "Stormbringer",
			"year" : 1974,
			"genre" : "Rock"
		}
	]
}

More Filtering Options

Here are some more ways of filtering results.

Specify AND Conditions

You can specify that only documents containing two or more specified values should be returned.

In this example, we specify that only musicians who play drums and where born before 1950 should be returned. Only documents that match both criteria will be returned.

Result:

{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }

Specify OR Conditions

You can also specify that either one or the other value should be true. As long as one of the conditions are true, the document will be returned.

In this example, we want documents that contain musicians that either play drums, or were born before 1950.

Result:

{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }

The $in Operator

The $in operator allows you to provide a list of values. If a document contains any of those values, it will be returned.

Using the following example, we're searching for all musicians who are either on vocals, or play guitar.

Result

{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }

Query an Array of Documents

This example queries an array of documents. It finds albums that were released after the year 2000.

Result:

{
	"_id" : ObjectId("578217c248ef8c6b3ffb015a"),
	"artistname" : "Robben Ford",
	"albums" : [
		{
			"album" : "Bringing it Back Home",
			"year" : 2013,
			"genre" : "Blues"
		},
		{
			"album" : "Talk to Your Daughter",
			"year" : 1988,
			"genre" : "Blues"
		}
	]
}
{
	"_id" : ObjectId("578217c248ef8c6b3ffb015b"),
	"artistname" : "Snoop Dogg",
	"albums" : [
		{
			"album" : "Tha Doggfather",
			"year" : 1996,
			"genre" : "Rap"
		},
		{
			"album" : "Reincarnated",
			"year" : 2013,
			"genre" : "Reggae"
		}
	]
}

You'll notice that these results also contain albums from earlier than 2000. This is correct — it's the way document-oriented databases work. Any query will return the whole document (but only those documents that match the specified criteria).

The db.collection.findOne() Method

You can use the db.collection.findOne() method to return one document that satisfies the specified query criteria.

If multiple documents satisfy the criteria, only the first one is returned, as determined by the natural order of the documents on disk.

So searching a whole collection like this:

Will only return one document:

{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }

If we change the findOne() to find() like this:

We see that there are actually 8 documents in the collection:

{ "_id" : 1, "name" : "Ian Gillan", "instrument" : "Vocals" }
{ "_id" : 2, "name" : "Ian Paice", "instrument" : "Drums", "born" : 1948 }
{ "_id" : 3, "name" : "Roger Glover", "instrument" : "Bass", "born" : 1945 }
{ "_id" : 4, "name" : "Steve Morse", "instrument" : "Guitar", "born" : 1954 }
{ "_id" : 5, "name" : "Don Airey", "instrument" : "Keyboards", "born" : 1948 }
{ "_id" : 6, "name" : "Jeff Martin", "instrument" : "Vocals", "born" : 1969 }
{ "_id" : 7, "name" : "Jeff Burrows", "instrument" : "Drums", "born" : 1968 }
{ "_id" : 8, "name" : "Stuart Chatwood", "instrument" : "Bass", "born" : 1969 }