- [Narrator] Now we are going to fetch the data from the API
and insert it into Mongo DB and always when working
with APIs, it makes sense to actually look
at the date that we will get back
and I already did this by just calling
the API without the start and the end date
this will give me the last seven days,
so that's long enough to understand the structure in that
just copy that, returned from
the API and in vicious to the code.
I will just create a new file data.Jason,
to when I save it, then I get formatting
and then I see how this data actually looks like.
We see that we have a property PPI
so this property PPI contains a list of dates
and the intercourse for this date.
And we want to create an entry for each date
so a new document in our database for each date.
So let's implement that in my Mongo back end chess,
I now scroll down to the insert method
and there I start with just fetching the data from the API
so at constant data equals await this coin API fetch.
Then I will create an empty ray for a documents,
so document equals square brackets and next I will iterate
over this PPI property of the return or check.
So I do object.entries for data that PPI
and for each of them I will get an entry
and for each entry I will now create
or add an element to a document ray
and this element should be an object
that can be inserted into the Mongo DB.
So document.push and we cannot just now insert
the ray here, so the date and the cost value,
so we have to create the structure that is more
in the form of some property and its value.
So enter date is the entry that we get
and it's the first element of this entry ray
and the value is the second element
so it's with the index one.
After that we have a full array with documents
and now I have to insert them into the database
of course we could also just iterate
over those entries and create a document
or like insert the document for each iteration
but that would not be really efficient for that.
Mongo DB gives us an operation that is called insert many
and that's exactly what we need
because that's a book operation that
will perform way better as if you would do it
(indistinct)
for each iteration so, I will now run
in the will return this, then return this collection.
So this is the collection object that we created when
we connect to the database
and on there, we have this insert many method enter
just to pass in the documents.
Of course we also have to call this now,
so after we connected to the database, I will now
add a new console info that says inserting into Mongo DB.
Then I will start the time again
so I start console time, Mongo DB-insert
and then we can already call the insert method
and we want to get the result back
so at const insert result equals await this top insert.
And then I will first stop the timer,
so add console that time and Mongo DB insert.
And we can use now this instant results to find
out how many documents were actually inserted
and for that and console the info
and we using string interpolation here
and I add inserted dollar colored brackets insert result
and there is a property result on there
and on this result object
there is a property and which is
the number of documents to the
(indistinct)
in our case inserted
and that can continue with documents into Mongo TB.
Let's see if this works, I'm running this
with NPM start and this looks pretty good.
Let's see, so we inserted 1788 documents into Mongo DB
I have a type there let's fix that documents
and this took around 291 or 292 milliseconds
that's pretty long, so you see
this is a rather expensive operation
and now you might wonder how the state is not represented
in Mongo DB and now we can look into Mongo DB compass
and up there is this icon to refresh the list of
databases and now we have a database Mex coin
and the collect values let's look into this collection
and you see that it's filled with date value pairs
and also it created automatically property ID
with the Mongo DB objected
which is the key that identifies
each Mongo DB document unique.
Now all the dismissing is to query that gives
us the maximum value from this database.