Welcome everybody, you're watching Mr Fugu Data Science.
Today, i wanted to create a beginner's tutorial for someone
who's never used mongodb. Now we're going to interface from R-studio to
mongo; using mongo lite. I will also show a few
shell commands. So before we get started let's look at the command line.
For the command line after you enter the mongo shell, there's a few
generic commands that you can do. You could look for
all of the databases by saying: show dbs. You can use a particular database
or the interesting thing with mongodb: you could create it on the fly
using the same command which is: use and then
the database name. I'm creating a database called video practice today.
It'll say you're switching to this database afterwards,
you could say: db. And it'll print out the current database that you're using.
From there, i want to create a collection. A collection
is basically like your table that you would think of in a
postgres or sql format. iIm calling my collection (vidPrac) or video
practice. It inserted correctly. Then i would
like to say: show me all of my collections that are
available. You do: show collections. Then we can
look at the databases once again and you'll see that it's available. When
we did it before, up here. You'll notice that while we
created a new database. It doesn't show up here until
we populate it with something. The same thing happens when you're dealing with
collections too, all right. After i created these i
wanted to do a few imports. For those who have
never used mongolite you're going to need to
install it with install.packages and then
call it in like this. We need to import the libraries of
tidyverse, knitter, markdown and mongolite, okay. Now
let's do a few things. Let's create our connection.
Okay, so i'm gonna just call mine: mongo connection. But, i'm just abbreviating it
and then we need to call mongo and then from there we need to call in
our collection. Which will be our vid prac and then my database that
i'm using. Which would be the video practice and then we're good to go.
Let's import these in real quick, there we go. And then this little
info thing. I'm not going to uncomment this because, it'll show personal
information. But, you could do this at your own accord.
Let's do a basic insert. So you have some data to play around with.
Let me just call this: i don't know fun data
and then let's create a let's create a vector: where we're
calling in what we want to create. I'm going to create a name
which would be the first and the last names.
And i'm going to call this person Mr. Bilbo. I'm going to call in the
last name of Mr. Bilbo; and his last name will be Baggins.
Mr. Baggins also, has a few things associated.
He has hobbies; and these hobbies will be interesting because, they are
a list this. Inside of this list: what are his hobbies? He likes
to find rings, he likes to go on adventures, he
likes to also: i'll just say he does magic. Let's
have some fun with it .Then we need to let's create one more
person so we can have a little fun with this.
And let's create somebody named Gollum and Mr. gollum. He
currently doesn't have a last name. So Mr. Golem doesn't have his last name
but, he does have some hobbies. And what are
his hobbies? He likes: to steal rings, he likes to
bite people, and what else would he like to do
talk to himself. Sure sounds nice, okay. If you notice here these are not of
the same size like something you would use
for a schema in postgres. You have a lot of flexibility using mongodb.
We need to send this data over. So how we're going to do that?
We need to first realize that there's an error here.
I missed something: so let's figure out where i missed this
something is awry, something is not right. There we are there we are you always
have to pay attention to the little nuances. Let's do our
insert statement; and this insert our fun little data. And
if you look we did two inserts. We didn't do anything
else perfect. So let's check this out, this current data out.
So let's do a find statement. Here you're leaving this open so it's taken all of
your parameters, every single field. A field you could
think of as a column. And we'll now we print out our data: and we see
that we have everything looking good. But, we don't
have last name for Mr. Gollum. We don't need to have a last name. But,
let's give him a last name and make him feel right at home with everybody else.
Alrighty, let's do our little trusty update statement. What are we going
to use for our update statement? Well we need to; we could do this a few
ways. Let's do it first a semi-correct way.
I say semi-correct because, i'm calling in
the first name of Gollum here. There's only two rows. But, what happens if
somebody had the same first name as him, and we were doing an
update by first name. We'd have an issue. Really we should use the ids that are
created and i'll show you that a little later.
But, let's just continue this for now, all right.
Let's call in Mr. Gollum and update by the first name.
And we have to set some values that we're going to change.
So you use in a set operator with this dollar sign ($). Then we need to create some
new brackets. And call a last name that we're going to create.
We're going to call him the lonely for his last name.
Just to be a little fun with this, and let's send that off and Mr.
lonely went through. What's interesting: is this should have been used,
and didn't need to be at this time. Which was interesting. When i did this
before it didn't work without having upsert
equals true. An upsert is basically allowing you to
update something that already exists. So that's interesting that this filled
in. Because, last time i did this i had to use this.
Nevertheless, we got a good old last name from Mr. Gollum our creepy little buddy.
Let's do some adding into our arrays okay. The arrays i'm talking about are
these right here for the hobbies.
Let's add into our arrays. Let's do this effectively because, we're
not going to add into every array. We're only going to add into Mr. Gollum
because, he has an extra hobby we need to take care of. So Mr. Gollum: let's give you
some fun. Let's find; we're going to say we're
going to find everything. But we're also going to do something
here. Which we haven't seen just yet. So let's find some fields
where we're going to call in the id and we're going to call
1. That means that we are calling it as true for it to print out
for a field. So Mr. Gollum: he has this right here.
Copy that because, that's his unique id. These are basically like
hashed ids. But, there's a significance about this and i've done it
in one of my previous videos on how this is
actually set up with your machine and kind of pseudo random stuff.
Anyway, so let's create this update for Mr. Gollum. How are we going to do
this? Well let's do an aggregate and see how
that's going to work. Okay, so this aggregate that we're going
to do is going to be a little involved. But we're going to try to match
on the id of our trusty old friend
Mr. Golem, where we're going to cut and paste
his id. But, we need to call in another little operator
and we need to do some more brackets for this
then we'll paste that in. Now what do we have to do because this is starting to
look ridiculous right. let's hit a comma and then start calling in what we're
going to start messing with. We need to do something called add a
field. And add a field allows you to basically
add a new column or add something to an array
just depends on what your situation is. So
let's do the add fields, and then we need to do another bracket.
And what are we adding into? We're adding into the hobbies:
where we are needing to use our special little operator again the dollar sign.
But, we're not quite there yet. Because, we also need to
establish what we're going to change. And we need to
concatenate the array. We're basically adding on to the end of our array, which
would be our hobbies. And what do we want to add into the
hobbies? We want to add in what his actual hobby is. And his hobby
is eating fish. That's what his hobby is.
Then, we can see if this is going to work
hopefully i didn't mangle this up. We just print off
everything. And sometimes you make a mistake and
then you fix your mistake, and there we are. So it says okay we
print off the id with our good old friend Mr. Gollum who
also likes to eat fish. But, if you noticed here there's
something interesting. Because, you don't see that eating fish
when you print off the query that has everything.
But your trusty old aggregate does. Well i didn't actually
update. I just ran a query, okay. Let's get into this: and actually
update our trusty old friend. He can have his
hobby established eating some fish. So let's do our update.
But, now we're going to call in our trusty old id. So we know who we're
calling in. Once again, we need to not forget our
little operator. And we're calling the oid which
is your object id, just a shorthand for it. And
then let's paste in our id. Now we're going to
use what's called a push operator, all right.
What are we doing with this push operator?
We're going to use this so we can insert into
our hobbies. So we're going to say he eats fish
once again. Then let's see if it'll do it without the upset
equals true. We get a little error because,
i didn't pay attention to something. There we are. Let's see if it updated. He
eats fish. That's so interesting, so i don't have to
say upsert equals true, huh. Okay so now we actually know that it
updated and says that he likes to eat fish.
Let's try some sorting this is quite a bit easier.
But you're going to see something lit goes awry
really fast. When you're doing some sorting you're going to find something
interesting. Let's first sort by the first
name and see what prints off. So we worked off first name golem. First name
Mr. Bilbo, they're in order. But if you notice the
fields or columns are not in the same order. How can we
rectify this? Let's try something: let's do last name.
Let's see if this works. If you notice this is kind of goofy
and it doesn't retain the order of the columns or features. So that's something
that's interesting. I think there's a way that you can
use an operator take care of this. One of the old operators
is deprecated that i know of. Something also i need to mention before, i forget.
If you're dealing with really large data sets and you're trying to do a sorting.
You're limited here: so make sure that you understand that. Because, you end up
getting error and it won't do it. In the event like that occurs: you
could try to rectify it by creating an index on something that was unique and
then you're sorting actually very easily in
constant time because you already have the index. That's just something to
take note of. We can select and find something
by its unique id: which we've already done a few times
with this just print that out. So let's do that. Let's call our
trusty old oid, covered up with some braces cut and
paste our good old buddy. Oh, and there we go.
You can also download files: which is really
convenient. So if you want to dump your database.
We could create a directory that will output some folder
within our current working directory to whatever
name we would like. So i'm calling my directory
output files practice. And you can see i already created one prior.
Then i could dump a file so that's let's call my file that i'm going to dump
and use an export statement. Then i'm going to call the file.
And then the file that i want to send. I'm sending this file to this folder
and then i'm just going to call it hobbies.json and send that off.
And you can see it'll send it off; and we can see it's right
there. There's our little file that we created. Now, we could also do some
deletes. What do we want to delete? Well let's
delete one of the rows that we were dealing with.
Let's do the Mr. Gollum; and let's delete him and see what that looks like.
If we just wanted to delete a row. We would
let's just cut all this so this actually this print this all off before
we do some deletes. Then let's do some deleting of Mr.
Gollum. Now, let's print this off and you can see Mr Gollum is
gone, now. If you want to drop the collection. But, you
don't want to... If you want to drop the data within the collection
but you don't want to drop the collection itself. You do a statement
like this. Where we would do; we would do the
remove once again. But, you make this simple, like this. So
this is going to delete everything within
our collection. It's going to be empty now.
So let's look. See it's empty; you can use the drop and
actually drops the whole collection itself. And now you don't have anything
in there as well. Some basic shell commands we went
over some of these in the beginning. But, for instance if you were dealing
with a weird name for your collection that was
separated by spaces or hyphens or something.
You can do a dot get collection. Which is really handy.
If you're working out of a shell script; you can exactly do
some of the functionality of use whatever database,
use whatever collection. But you can do: a get sibling and then you just be
working from your current db. You would put in here and get the
sibling and that would work and then that would
give you flexibility of getting neighboring
databases. So you could work with other collections.
Then if you just needed to exit in the command line or terminal
you just do a quit, if you're in there. And then you can always run a help
if you need to. Those are some of the basic commands that are really useful.
I didn't do any indexing for this tutorial one basically because
there's only two rows, two there was nothing important to index
on, and 3 it was giving me issues with the arrays.
I would like to say thank you for watching.
Please like, share, and subscribe and if you subscribe
turn on that notification bell. I hope this brought utility to somebody.
As always i'll see you in the next video.
Bye