Now it's time to talk about reading data from the MongoDB database. The MongoDB command line interface provides lots of methods for it. In this topic, you will learn how to get several records or just a single document from the database and sort it by your criteria. Let's start!
Getting several records
First of all, let's create a collection named customers with such fields as _id, first_name, last_name, city, and address. Then let's fill it with some data:
{ _id: 1, first_name: "Adam", last_name: "Johnson", city: "Washington", address: "4695 Passaic Street" }
{ _id: 2, first_name: "Lisa", last_name: "Carlton", city: "Milwaukee", address: "4076 Hartland Avenue" }
{ _id: 3, first_name: "Michael", last_name: "Schmidt", city: "Washington", address: "1986 Eagle Drive" }
{ _id: 4, first_name: "Francine", last_name: "Poe", city: "Falmouth", address: "4306 Kovar Road" }Great! Now let's get all the data from this collection. To do that, we need to use the find() function:
db.customers.find()The output will be the following:
{ _id: 1, first_name: "Adam", last_name: "Johnson", city: "Washington", address: "4695 Passaic Street" }
{ _id: 2, first_name: "Lisa", last_name: "Carlton", city: "Milwaukee", address: "4076 Hartland Avenue" }
{ _id: 3, first_name: "Michael", last_name: "Schmidt", city: "Washington", address: "1986 Eagle Drive" }
{ _id: 4, first_name: "Francine", last_name: "Poe", city: "Falmouth", address: "4306 Kovar Road" }All our records were outputted exactly as we wanted them to.
Now, imagine, we need to get the data based on some criteria. For example, let's get all the customers from Washington. For this, we can set a necessary argument to the find() function, written in JSON format:
db.customers.find({city: "Washington"})In this case, we get two customers from Washington:
{ _id: 1, first_name: "Adam", last_name: "Johnson", city: "Washington", address: "4695 Passaic Street" }
{ _id: 3, first_name: "Michael", last_name: "Schmidt", city: "Washington", address: "1986 Eagle Drive" }Note, that you can add as many arguments as you want. For example, let's get the record about the person with the name Francine that lives in Falmouth:
db.customers.find({first_name: "Francine", city: "Falmouth"})The output will be the following, exactly as we wanted:
{ _id: 4, first_name: "Francine", last_name: "Poe", city: "Falmouth", address: "4306 Kovar Road" }Getting the single record
Now let's imagine that we need to get only one record from the collection. The collection will be the same as in the previous example. In order to get a single record, we need to use the findOne() function. This function always takes an argument(s) with search criteria.
In this example we want to get some information about Lisa:
db.customers.findOne({first_name: "Lisa"})We get the following record:
{ _id: 2, first_name: "Lisa", last_name: "Carlton", city: "Milwaukee", address: "4076 Hartland Avenue" }Amazing! But what if we have multiple records with the same criteria? In this case, the findOne() method returns the first document according to the order of the records.
Remember these two folks from Washington? Let's search by this criterion again using the findOne() method:
db.customers.findOne({city: "Washington"})We will get only one person, Adam Johnson, who is the first in this collection:
{ _id: 1, first_name: "Adam", last_name: "Johnson", city: "Washington", address: "4695 Passaic Street" }Making it prettier
When the document has too many fields, it can be hard to read and understand the data. Have a look at this record below:
{ _id: "6249764d26130150e0bc3ba1", index: 0, isActive: false, balance: "$3,969.38", picture: "http://placehold.it/32x32", age: 34, name: "Pittman Mcmillan", gender: "male", company: "COMBOGEN" }Doesn't look very good, right? Well, we have a solution! The pretty() method will help us make the document structure more readable.
Use it with the find() or findOne() method like so:
db.<collection>.find(<query>).pretty()And you will see a much better result than before:
{
_id: "6249764d26130150e0bc3ba1",
index: 0,
isActive: false,
balance: "$3,969.38",
picture: "http://placehold.it/32x32",
age: 34,
name: "Pittman Mcmillan",
gender: "male",
company: "COMBOGEN"
}Conclusion
Good job! In this topic, we've learned about reading data in the MongoDB database. Let's repeat some key facts, so you will be ready for practice!
Use
find()to get all the records from the collection.Set arguments for
find()to search by some criteria.Use
findOne()to get only one document.Use
pretty()to make the output more readable.