Retrieving documents

To retrieve the list of documents, we use the find method. For example, run the following query to retrieve the full list of teams:

> db.teams.find()

{ "_id" : ObjectId("5a5cf1419afc8af268b9bb21"), "code" : "PER", "name" : "Peru", "ranking" : 11, "captain" : "Paolo Guerreo", "Trainer" : "Ricardo Gareca", "confederation" : "Conmebol" }

As you can see, a new _id property has been added automatically. This property is known as the primary key of the JSON document. This is an autogenerated value, so you will have a different value when you run the command.

The teams' collection over time will have more than just a single team. So, how could we retrieve a single team from the collection? Do you remember the {} JSON object we passed to the find method? This JSON object is used to query the collection. So, if we want to retrieve the Peru team, we have to execute the following query:

> db.teams.find({"code": "PER"})

{ "_id" : ObjectId("5a5cf1419afc8af268b9bb21"), "code" : "PER", "name" : "Peru", "ranking" : 11, "captain" : "Paolo Guerreo", "Trainer" : "Ricardo Gareca", "confederation" : "Conmebol" }

Note that we can pass any field used in the JSON document. For example, you can use name, ranking, captain, and so on.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset