Interacting with cursors
When we compose a query, Mongo gives us back a cursor object from which we can get the values.
When we called limit and sort in the last section, we were actually calling methods on the cursor that was returned by find.
If we save the cursor in a variable we can call more methods on it.
var people = db.people.find();
We can iterate over the the cursor using a simple while loop. We can check if the cursor has a next value, and can call cursor.next to get the value out.
var people = db.people.find();
while (people.hasNext()) {
print(tojson(people.next()));
}
Functional Loops - forEach and map
We can simplify the code above using functional style loops.
db.people.find().forEach(function (person) {
print(person.name);
});
We also have access to map, which will return an array of values.
var array = db.people.find().map(function (person) {
return person.name;
});
Exercise - Cursor methods
You can read all the cursor methods here:
http://docs.mongodb.org/manual/reference/method/js-cursor/
- Iterate over each of the people and output them.
- change the find function to find only the people with cats.
- Iterate over each of the people, outputting just the cat name and age each time.
- Use Map to generate an array containing all of the cat names.
Exercise - Stock ticker
- Sort the stocks by profit
- Now iterate over the cursor and output all of the stocks names and tickers in order of profit.