We've worked with our flight data quite a bit,
now let me add another kind of data. I'll clear the console with the cls command for that
and now I'll add a passenger's collection to my flight's database
and I want to insertMany passengers in there. Now you find a file attached to this video, the
passengers.json file with an array of passenger data,
just some dummy names and ages.
Copy that and add it here and add a closing parentheses at the end and hit enter
and this should insert many elements with the insertMany command with all these automatically generated
IDs.
Now we can have a look at that data by accessing passengers and you can always just type and then use
tab auto-completion here by the way, so simply hit tab in the shell
and it should auto-complete, you can find them and pretty print the results,
there are our passengers. Now on first sight, this might look good but actually if you have a look at the
last entry, Albert Twostone and then you have a look at the data you found attached,
you will see that Albert is not the last entry,
Gordon Black actually is. Now the order
here is the same order as it was added to the database,
so where is Gordon?
Well you also see the type it for more,
if we type it, then we see Gordon Black.
Now this it command is a feature shown in the shell,
I will show you another way of getting the data
if you were using a mongodb driver in an application in a second
but the fact that we had used that is not shell specific, instead find,
so passengers find or the find command in general no matter on which collection you use it gives you
back a cursor
and not all the data.
Previously, it looked like it always gives us back all the data
but actually it gives us a so-called cursor object,
now what is that? Find does not give us an array of all the documents in a collection and that makes
a lot of sense because that collection could be very big
and if find would immediately send us back all documents and you think about a collection with let's
say 20 million documents, then this would take super long but most importantly, it would send a lot of
data over the wire,
so instead of that, it gives us back cursor object which is an object with a lot of metadata behind it that
allows us to cycle through the results
and that is what that it command did,
it basically used that cursor to fetch the next bunch of data.
So find here really just gives us back that cursor and we can actually see that by using some other methods
here,
for example there is toArray,
if you execute this, now you see Gordon Black as the last element
and now there is no type it to see more
because toArray will actually go ahead and exhaust the cursor,
so go through the entire list and fetch all the documents and not stop after the first 20, which by the way
is simply a feature by the mongodb shell,
it gives you the first 20 documents automatically but then stops, toArray simply gets them all and gives
you an array.
Now that might be fine here but of course that is also not optimal if you've got a lot of elements. In
your application code, you would most likely use something like passengers find
and then there is a forEach method and forEach here simply allows you to write some code to do something
on every element that is in your database, on every document you got in there.
Now the exact syntax you can use is something you can find in the driver docs for your driver,
so if you're using PHP, check out the PHP mongodb driver docs and you will see how to use that
forEach function. In javascript and the shell is based on javascript,
you could pass a javascript function to it using an arrow function here where you will get an argument
passed into the function automatically and that will be your document and you can name this argument
however you want,
so in our case it will be our passenger data
and then you can use it in here and here in the shell, there is a print json command which we can use
to pass or to well yeah pass our json data which we want to output like passenger data.
And now we also get all the documents because now what we're doing is on the cursor which find does
return, we execute the built-in forEach method to execute this method here,
this anonymous method on every element and the cursor will simply go through our entire collection,
fetch all documents one at a time, execute that function on it, pass the document into the function
and then here we just happened to print it out like this in your application with your favorite language.
You can of course do to the data whatever you want to do, render it onto a html page, send it with an
http request, whatever you need to do
and that is just what I want you to take away from this lecture.
Find does not by default give you all the documents even though it looked like it did because the shell
actually gives you the first 20 documents by default,
instead find gives you a cursor and then it's up to you to use that cursor with toArray to force it
to get all documents and put them into an array or better for large sets of documents, with forEach
and there are more methods available which you find in your driver docs.
Technically you need to know that with forEach, it will really just fetch the next document for every loop
cycle,
so this is very efficient here because it does not fetch all the data in advance, load it into memory
and then force you to go through it with forEach, instead it really just fetches the data on the demand
therefore of course not overusing your bandwidth and not loading too much into memory.
So this is the important thing to take away,
find gives you a cursor and not the element itself.
This also is the reason why on find one, we could not use pretty, you remember if I use passengers find
one, this works, find one pretty fails because pretty is a method that simply exists on the cursor here.
So this is why find one does not support it
because find one does not give us a cursor because it only gives us one document anyways.
Find gives us multiple documents,
therefore we don't get the data immediately,
we get the cursor instead, the shell happens to take that cursor and give us the first 20 documents by default
but we can also control that with forEach or toArray
and here, pretty exists because it is simply a method that exists on the cursor to pretty print the output.
This is what you need to take away here, for the other methods like insert, update and delete, cursors
do not exist because these methods don't fetch data,
they manipulate it
instead.