- 00:00:02 welcome to this video
- 00:00:03 in this video we'll have a look at one
- 00:00:05 of the most important and trending
- 00:00:06 databases you can work with
- 00:00:08 as a web developer these days mongodb
- 00:00:11 we'll have a look at what it exactly is
- 00:00:14 how it works how we can use it and
- 00:00:16 what belongs to its rich ecosystem
- 00:00:18 because that's
- 00:00:19 so much more than that little database
- 00:00:22 you can install locally on your system
- 00:00:24 so we'll have a look at all these things
- 00:00:26 and after this video you'll already be
- 00:00:29 ready to start
- 00:00:30 using it in your next applications so
- 00:00:32 let's start
- 00:00:36 so what is mongodb well
- 00:00:40 first let's start with the name it comes
- 00:00:42 from the english word
- 00:00:43 humongous which basically means that it
- 00:00:46 can store
- 00:00:47 lots and lots of data and that is why
- 00:00:49 that name
- 00:00:50 was chosen so humongous basically means
- 00:00:53 that there is a plenty of it
- 00:00:55 now in germany that's actually a bit of
- 00:00:57 a strange name but
- 00:00:58 just as a side note maybe you can find
- 00:01:00 out why but this is where the name comes
- 00:01:02 from
- 00:01:03 the core idea behind mongodb
- 00:01:06 is that we can store a lot of data in
- 00:01:09 there
- 00:01:10 and work with it in a very performant
- 00:01:12 way
- 00:01:13 so that we can write with high
- 00:01:15 throughput and retrieve data very
- 00:01:17 efficiently and fast
- 00:01:19 i'm talking about milliseconds here
- 00:01:21 which is of course super important for
- 00:01:23 modern
- 00:01:24 web and mobile applications where you
- 00:01:26 typically use mongodb
- 00:01:27 because you can deliver data to your
- 00:01:29 users fast
- 00:01:31 now i also got a video by the way where
- 00:01:33 i compare
- 00:01:34 nosql databases as mongodb is as you
- 00:01:37 will learn
- 00:01:38 and sql alternatives like my sequel
- 00:01:41 so definitely check out that video and
- 00:01:43 article to learn more about when to
- 00:01:45 choose which
- 00:01:46 in this video i'll obviously focus just
- 00:01:48 on mongodb
- 00:01:49 so how does it work then well it's a
- 00:01:51 database and therefore it's all about
- 00:01:54 well storing data we might have a shop
- 00:01:57 database so in our mongodb
- 00:02:00 database server which we start in the
- 00:02:02 end you will learn how to install it and
- 00:02:04 how to
- 00:02:05 bring up such a server in this video on
- 00:02:07 that server you can have multiple
- 00:02:09 databases and let's say we have a shop
- 00:02:11 database
- 00:02:12 because we're building an online shop
- 00:02:15 now in that database
- 00:02:16 we then have something called
- 00:02:18 collections we might have a users
- 00:02:20 and orders collection if you're coming
- 00:02:22 from a sql world
- 00:02:24 collections would be tables so
- 00:02:27 collections are basically the
- 00:02:29 parts of our database where we put our
- 00:02:31 data into
- 00:02:32 now the data itself would be a row or
- 00:02:35 entry
- 00:02:36 in a sql database in mongodb world
- 00:02:40 we're talking about documents which look
- 00:02:42 something like this
- 00:02:44 if you're working with javascript then
- 00:02:46 you might recognize these curly braces
- 00:02:49 that looks a lot like javascript objects
- 00:02:52 and indeed mongodb uses a notation or a
- 00:02:56 data format
- 00:02:57 which they call bson very similar to
- 00:03:00 json i'll come back to that in a second
- 00:03:02 which looks a lot like json and actually
- 00:03:04 also works a lot like it
- 00:03:06 so we store such documents in there and
- 00:03:08 one important thing and one
- 00:03:10 key difference to sql databases
- 00:03:14 is that these documents are schema-less
- 00:03:17 which means as you can see here we can
- 00:03:19 have one collection
- 00:03:20 one table if you want to call it like
- 00:03:22 that where we store
- 00:03:24 different data in here we have one
- 00:03:26 document which describes a user
- 00:03:28 who has a name and an age and the other
- 00:03:31 user only has a name because maybe we
- 00:03:33 didn't retrieve the h
- 00:03:34 data in mongodb it's no problem to store
- 00:03:38 such mixed data in one and the same
- 00:03:40 collection
- 00:03:41 which makes mongodb very very flexible
- 00:03:44 and it's especially great for example
- 00:03:46 for growing businesses where you don't
- 00:03:49 know which kind of data you will need in
- 00:03:51 the end or you will be able to gather in
- 00:03:54 like six months
- 00:03:55 but you want to start now and you can
- 00:03:57 basically store the data
- 00:03:58 you have and if you collect more data in
- 00:04:00 six months you can add it to the same
- 00:04:02 collection
- 00:04:03 obviously you'll have to work on your
- 00:04:04 querying logic then and
- 00:04:07 make sure you don't rely on data which
- 00:04:09 might not be there for all documents
- 00:04:11 but in general this is a great advantage
- 00:04:13 or it can be a great advantage at least
- 00:04:16 now i was referring to that data format
- 00:04:19 which is called bsun which basically
- 00:04:20 stands for binary
- 00:04:22 json now json is a data format you know
- 00:04:25 from javascript or from web development
- 00:04:27 in general
- 00:04:28 it's a data format that looks pretty
- 00:04:29 much like what you see here
- 00:04:31 like a javascript object in the end you
- 00:04:32 could say now bsun
- 00:04:35 binary json is in the end using the same
- 00:04:38 format but it's binary encoded to make
- 00:04:42 working with it behind the scenes on the
- 00:04:44 server more efficient
- 00:04:45 to make it work with basically all
- 00:04:49 languages you might use to reach out or
- 00:04:50 to connect to your mongo database
- 00:04:52 so it gives you more flexibility but you
- 00:04:55 don't have to worry about
- 00:04:56 that you in the end store objects in
- 00:04:58 your database that look like that
- 00:05:00 and you will see how we create them in
- 00:05:02 just a minute
- 00:05:04 now the cool thing we can see here is
- 00:05:06 we're really flexible regarding the
- 00:05:08 data format and i said that you can mix
- 00:05:12 data structures in the same collection
- 00:05:14 you can also have
- 00:05:15 so called embedded documents so here we
- 00:05:18 have one document
- 00:05:20 right with name agent and address but
- 00:05:22 the address is a so-called embedded
- 00:05:25 document so it's a document in a
- 00:05:27 document
- 00:05:27 and that allows us to keep data where we
- 00:05:30 need it
- 00:05:31 and that's another important difference
- 00:05:32 to sql databases
- 00:05:34 there we split data across tables we
- 00:05:37 might have a users table and then an
- 00:05:39 address table
- 00:05:40 and we would match them with the id but
- 00:05:43 here
- 00:05:43 the idea is that we put data that
- 00:05:46 belongs together
- 00:05:47 into one document now there are
- 00:05:49 different strategies for distributing
- 00:05:51 data too but generally you want to keep
- 00:05:53 data together
- 00:05:54 because that makes retrieving it and
- 00:05:57 working with it
- 00:05:58 much easier because you already got
- 00:06:00 everything you need without complicated
- 00:06:02 joints which cost performance
- 00:06:05 or which can cause performance at least
- 00:06:07 so here we got all we need and we can
- 00:06:08 just retrieve it
- 00:06:10 and we don't need to join multiple
- 00:06:11 collections or tables
- 00:06:13 you can also have multiple nested
- 00:06:17 documents like for the hobbies here
- 00:06:18 which is an array of nested documents so
- 00:06:21 this is all possible this is really
- 00:06:23 flexible
- 00:06:24 and working with it is a lot of fun
- 00:06:26 because especially if you're coming from
- 00:06:28 a javascript world but you can work with
- 00:06:30 mongodb
- 00:06:30 just fine from php pi finance onto but
- 00:06:34 if you're coming from a javascript world
- 00:06:36 this is all syntax or code you already
- 00:06:38 know
- 00:06:39 and therefore working with it is really
- 00:06:41 really simple
- 00:06:42 and fast not the worst combination
- 00:06:45 now one important note of course has to
- 00:06:47 be made about relations
- 00:06:49 since we generally keep our data in one
- 00:06:52 place
- 00:06:53 we don't really have relations across
- 00:06:56 multiple collections
- 00:06:57 though we can have them but if we have
- 00:06:59 them
- 00:07:00 we basically have to merge them uh
- 00:07:03 manually we
- 00:07:04 basically have to write queries where we
- 00:07:05 retrieve the first documents and then
- 00:07:07 reach out to the naval collection
- 00:07:09 and find the fitting connect the
- 00:07:10 documents on our own
- 00:07:12 but often you don't have that split up
- 00:07:15 approach
- 00:07:16 but you can do that too in mongodb now
- 00:07:18 that's enough of the theory for now time
- 00:07:20 to get our hands
- 00:07:21 dirty so let's visit mongodb.com and see
- 00:07:24 how we can get started using it
- 00:07:26 on mongodb.com we can see that we can
- 00:07:28 get started for
- 00:07:30 free which is not the worst if we click
- 00:07:32 that button here
- 00:07:33 we're taken to a page where we actually
- 00:07:35 get suggested to get
- 00:07:37 to use mongodb atlas now i will actually
- 00:07:40 use that in that video
- 00:07:41 mongodb atlas is a cloud-managed mongo
- 00:07:45 database so a database running in the
- 00:07:46 cloud to which we still can connect from
- 00:07:48 our local machine
- 00:07:50 or from any app we're writing now you
- 00:07:52 don't have to use that cloud solution
- 00:07:54 though
- 00:07:55 you can also install it locally in the
- 00:07:57 top right if you click on get
- 00:07:59 mongodb then
- 00:08:02 you also start with the atlas selected
- 00:08:04 but you can switch to the community
- 00:08:05 server which is the
- 00:08:07 freely available server you can install
- 00:08:09 locally on your machine
- 00:08:11 here you just have to choose your
- 00:08:12 operating system
- 00:08:14 and then download and install it and
- 00:08:16 make sure to follow these installation
- 00:08:18 instructions in case you're facing any
- 00:08:20 errors or in case you just want to well
- 00:08:22 make sure that you get it right
- 00:08:24 now there's nothing wrong with
- 00:08:25 installing that locally and it
- 00:08:27 might be the first intuition you have
- 00:08:30 but
- 00:08:30 actually the atlas server gives you
- 00:08:34 in my opinion uh first of all an easier
- 00:08:37 way of starting because it's really
- 00:08:39 simple it's also free to get started
- 00:08:41 with that
- 00:08:41 that's all important to mention and it's
- 00:08:44 also more realistic
- 00:08:45 because once you finished working on
- 00:08:48 your
- 00:08:48 let's say node.js app or whatever it is
- 00:08:51 where you connect to mongodb
- 00:08:53 once you've finished with that you will
- 00:08:56 need some kind of deployed mongo
- 00:08:58 database and deploying a mongo database
- 00:09:01 just like any database
- 00:09:03 is a more complicated process because
- 00:09:05 obviously you want to have a scalable
- 00:09:07 performant secure database and mongodb
- 00:09:10 atlas does all of that for you
- 00:09:12 it's easy to set up sharding there which
- 00:09:14 basically is the process of
- 00:09:16 using multiple servers that are
- 00:09:18 connected together to store and retrieve
- 00:09:21 and work with your data
- 00:09:22 you also get replica sets there which
- 00:09:24 basically means that your data is
- 00:09:26 replicated across
- 00:09:27 multiple nodes for fault tolerancy so
- 00:09:31 that when one node
- 00:09:32 fails or is under heavy pressure you can
- 00:09:35 get the data
- 00:09:36 from another node you also can retrieve
- 00:09:38 it faster therefore because you can
- 00:09:40 work with multiple nodes with the same
- 00:09:42 data simultaneously
- 00:09:43 so all these things come for free with
- 00:09:45 such a setup
- 00:09:46 you have to manage them on your own when
- 00:09:48 working with the community server
- 00:09:50 the enterprise server by the way is
- 00:09:52 basically the same but with a commercial
- 00:09:53 license
- 00:09:54 so if you want to have a self-managed
- 00:09:56 server and you want to earn money with
- 00:09:57 it
- 00:09:58 that would be the option to go or there
- 00:10:00 too you can use
- 00:10:01 atlas so i will use atlas because it's
- 00:10:03 easier to get started
- 00:10:05 and does a lot of heavy lifting for us
- 00:10:07 you just need to sign up there but you
- 00:10:09 don't even need a credit card and it
- 00:10:10 will be for free
- 00:10:12 and once you did sign up which i already
- 00:10:14 did
- 00:10:15 you either end up in the set up your
- 00:10:18 furnace cluster wizard already or
- 00:10:21 on a screen like this where in the end
- 00:10:23 you can build a new cluster
- 00:10:25 now here you basically set up your cloud
- 00:10:27 mongo database
- 00:10:28 and we can use the standard settings
- 00:10:30 here because these are the settings
- 00:10:32 which will give us a free
- 00:10:33 mongodb clustering database now you can
- 00:10:37 obviously also switch to more advanced
- 00:10:39 settings if you need that for production
- 00:10:41 you can split your database across
- 00:10:43 multiple regions worldwide you have the
- 00:10:45 fastest
- 00:10:46 retrieval time and latency for the
- 00:10:48 different regions your users might be in
- 00:10:50 you can choose from different cloud
- 00:10:52 providers because it's cloud hosted
- 00:10:54 and not on mongodb's own servers but
- 00:10:57 instead they're
- 00:10:58 using these cloud providers there you
- 00:11:00 can use the region or choose the region
- 00:11:02 where
- 00:11:03 your mongo database will be hosted in
- 00:11:05 and i will leave the default
- 00:11:07 make sure to choose an option with free
- 00:11:09 tier available to well get this
- 00:11:11 for free then you have to choose the
- 00:11:13 cluster tier
- 00:11:14 and here we only got one free clustered
- 00:11:16 here which is this
- 00:11:18 very simple one which is fine for
- 00:11:20 development and testing purposes though
- 00:11:22 but of course you can also ramp this up
- 00:11:25 to get
- 00:11:26 better faster clusters with more storage
- 00:11:29 space where you can store
- 00:11:30 more data and retrieve it faster and you
- 00:11:32 see the prices here but i'll go with the
- 00:11:34 free alternative
- 00:11:36 and then you have some additional
- 00:11:38 settings down there like the mongodb
- 00:11:40 version now on the free cluster we are
- 00:11:43 stuck to
- 00:11:44 this version here now that will
- 00:11:45 eventually also change in the future but
- 00:11:47 right now it's 3.6
- 00:11:49 on paid clusters you can actually then
- 00:11:52 all
- 00:11:52 choose the latest version for example
- 00:11:54 but for our purposes here
- 00:11:56 switch back to the free cluster i'm fine
- 00:11:59 with that version which
- 00:12:00 is the second most recent now on
- 00:12:03 better or more expensive options here
- 00:12:07 tiers up there we could also add or
- 00:12:10 backup and shard our clusters which is
- 00:12:13 the process of using multiple connected
- 00:12:15 servers
- 00:12:15 and that's not to be mistaken with the
- 00:12:17 replica sets where we have
- 00:12:19 fault tolerancy sharding basically means
- 00:12:23 that we get even more power because we
- 00:12:26 basically
- 00:12:27 duplicate the number of servers that are
- 00:12:30 working together we don't just have
- 00:12:32 multiple data storage nodes
- 00:12:34 but we really have the power times two
- 00:12:36 or three or how many shards we add
- 00:12:39 so we could do that um if we have that
- 00:12:41 tier or higher
- 00:12:43 and so on so you can see what you can
- 00:12:45 add we need none of that
- 00:12:47 and therefore we can just give that a
- 00:12:49 name you can go with the default or
- 00:12:51 choose your own and then click
- 00:12:52 create cluster confirm that we're not a
- 00:12:55 robot
- 00:12:56 and then let this spin up and now this
- 00:12:59 will take a little while
- 00:13:01 and in the meantime as it also suggests
- 00:13:03 here we can go to security
- 00:13:05 because we want to secure our cluster
- 00:13:08 here
- 00:13:09 and that basically means we want to
- 00:13:10 control who can access it
- 00:13:12 obviously we want to lock that down only
- 00:13:15 our application should be able to access
- 00:13:17 that
- 00:13:17 because well we're probably storing some
- 00:13:19 sensible data in there
- 00:13:21 now what can we do here you can create
- 00:13:24 different users with
- 00:13:25 different permissions here give them
- 00:13:27 username and a password
- 00:13:30 you can then choose what they can do can
- 00:13:32 they only read read write or
- 00:13:33 administrate everything i already got a
- 00:13:36 couple of users there as you can see
- 00:13:38 you can add an ip white list and there
- 00:13:41 you can basically control which ips are
- 00:13:44 able to connect to that cluster
- 00:13:47 now i got a couple of ips there and that
- 00:13:49 might be or
- 00:13:50 probably is empty for you and then you
- 00:13:52 can just add an ip address
- 00:13:54 and enter your current ip address of the
- 00:13:56 local machine you're connecting
- 00:13:59 to this so that you can um well work
- 00:14:02 with that
- 00:14:03 and you can also simply click add
- 00:14:05 current ip to automatically retrieve
- 00:14:06 that
- 00:14:07 or allow access from anywhere which you
- 00:14:10 don't want to use in production
- 00:14:12 so we can confirm that add that ip
- 00:14:14 address and we can ignore the other
- 00:14:17 settings here for now
- 00:14:18 so this now gives us a setting where
- 00:14:20 only our
- 00:14:21 local machine only we can connect to
- 00:14:24 this cluster
- 00:14:25 and only with one of the users we
- 00:14:26 created here and if you forgot your
- 00:14:28 password you can always edit this and
- 00:14:30 even audit generate
- 00:14:32 a secure one automatically make sure to
- 00:14:34 well remember your password you will
- 00:14:36 need it for connecting to that cluster
- 00:14:38 of course
- 00:14:39 now with that let's go back to overview
- 00:14:41 and let's wait for this to finish before
- 00:14:43 we then finally connect
- 00:14:45 to this cluster and start writing some
- 00:14:48 queries so that you can not only see how
- 00:14:50 to
- 00:14:50 get it all started but how it actually
- 00:14:53 works
- 00:14:54 so now my cluster creation finished and
- 00:14:55 now it's time to actually connect
- 00:14:58 now if you click on connect here you
- 00:15:00 actually gotta get a little introduction
- 00:15:02 how that works and
- 00:15:03 most importantly you have to choose a
- 00:15:05 connection method
- 00:15:06 now what does that mean typically you of
- 00:15:08 course use mongodb
- 00:15:10 from your application you're creating
- 00:15:12 and that could be an application written
- 00:15:14 with node.js
- 00:15:16 with php asp.net python whatever it is
- 00:15:20 not directly from angular or reactor or
- 00:15:23 so on
- 00:15:24 because you will need a server-side
- 00:15:26 application connecting to mongodb
- 00:15:28 for both technical and security issues
- 00:15:31 because you don't want to expose your
- 00:15:33 credentials
- 00:15:34 in your client-side code which would be
- 00:15:36 the case in angular or react
- 00:15:38 therefore we either have such an
- 00:15:40 application if you click on connect to
- 00:15:42 application you actually can choose
- 00:15:44 which language you used and these are
- 00:15:46 not all languages which are supported
- 00:15:48 you can click on view all mongodb driver
- 00:15:50 connection examples to get
- 00:15:52 the complete list and if you click on
- 00:15:54 that on node for example
- 00:15:55 you get forwarded to a page where you
- 00:15:58 find instructions
- 00:15:59 on how to install and use the node.js
- 00:16:02 mongodb
- 00:16:02 driver so here you would have to
- 00:16:05 basically
- 00:16:06 choose your version to get detailed
- 00:16:09 instructions you then
- 00:16:11 find out how to connect in node.js with
- 00:16:13 the mongodb driver
- 00:16:14 and obviously you need to install that
- 00:16:16 driver too but i'll have a look at this
- 00:16:18 in a separate video
- 00:16:19 they're basically for node is a npm
- 00:16:21 package an official one which you add to
- 00:16:23 your project
- 00:16:24 and you're good to go and you can
- 00:16:26 basically start importing the mongodb
- 00:16:29 package here and access the client so
- 00:16:32 that is how you would connect
- 00:16:34 in that way but i actually want to
- 00:16:36 connect differently so i'll bring this
- 00:16:38 up again
- 00:16:39 i will use the mongo shell and that is
- 00:16:41 basically a
- 00:16:42 javascript based program which you run
- 00:16:45 in your terminal or
- 00:16:46 command prompt on your machine where you
- 00:16:49 can write all these query commands
- 00:16:51 and connect to your server be that a
- 00:16:53 local server or that cloud-based server
- 00:16:55 here
- 00:16:56 and therefore you can play around with
- 00:16:58 it in a very very simple way
- 00:17:00 without creating a whole node
- 00:17:02 application just to play around with it
- 00:17:05 we also got mongodb compass and we'll
- 00:17:07 have a look at this in a while
- 00:17:09 this is basically a tool which gives us
- 00:17:11 a visual
- 00:17:12 interface which is great but we want to
- 00:17:14 use the console because there we write
- 00:17:16 the queries pretty much as we'll
- 00:17:18 also write them in our apps in our node
- 00:17:21 or php or whatever it is apps
- 00:17:23 we would connect to mongodb so therefore
- 00:17:26 we'll have a look at compass later with
- 00:17:27 the graphical ui for now
- 00:17:29 let's simply use the setup where we
- 00:17:32 write the code which is close to the
- 00:17:33 code you would use in a real application
- 00:17:36 here you have to choose your operating
- 00:17:37 system and then download
- 00:17:40 the shell first of all so this will now
- 00:17:42 download it to my
- 00:17:43 system and installing it is very simple
- 00:17:47 you can view the detailed instructions
- 00:17:48 here to then also find out how to
- 00:17:50 connect
- 00:17:51 for that you just have to choose the
- 00:17:52 version and if you just downloaded it
- 00:17:54 you'll use
- 00:17:55 shell version 3.6 or later keep that
- 00:17:58 open
- 00:17:58 let's now install that shell
- 00:18:02 and the installation will differ from
- 00:18:04 operating system to operating system
- 00:18:07 in the video description you also find a
- 00:18:09 link with some information on how that
- 00:18:10 works
- 00:18:11 in the end on mac you just extract a
- 00:18:14 file
- 00:18:15 and then you get a folder which contains
- 00:18:19 the shell and you can move that folder
- 00:18:22 anywhere you want
- 00:18:23 and simply navigate into that folder in
- 00:18:25 your terminal
- 00:18:26 and start it and you started with this
- 00:18:30 command actually to connect to this
- 00:18:32 cluster for that given user here and on
- 00:18:35 windows it's very similar by the way
- 00:18:38 so i navigated into that bin folder that
- 00:18:40 was extracted on mac
- 00:18:42 on windows you'll have an installer once
- 00:18:44 you walk through that you'll also have a
- 00:18:46 folder where the shell is installed
- 00:18:48 and then you execute that mongo command
- 00:18:51 which is in there you can just copy and
- 00:18:53 paste in the command you got in that
- 00:18:55 modal on the mongodb cluster but there
- 00:18:57 is something you have to add
- 00:18:59 on mac you have to add dot slash at the
- 00:19:02 beginning
- 00:19:03 now this will try to connect to your
- 00:19:05 cluster and ask you for your password
- 00:19:07 that password is something you set up
- 00:19:10 for the user if you ever forgot it you
- 00:19:12 can of course assign a new one
- 00:19:13 and then enter that password here and it
- 00:19:16 should now connect to your cluster
- 00:19:18 now once this finished you should be
- 00:19:20 connected to the cluster and therefore
- 00:19:21 to the database server
- 00:19:23 not to a single database now you can
- 00:19:25 start running mongodb commands and of
- 00:19:27 course you find all available commands
- 00:19:29 in the official docs
- 00:19:30 one useful one is show dbs it shows you
- 00:19:33 all the databases you have on your
- 00:19:34 cluster and if it's brand new
- 00:19:36 you'll have these two now on mongodb
- 00:19:39 which is pretty convenient you don't
- 00:19:41 have to create databases with complex
- 00:19:43 commands
- 00:19:44 instead they are created on the fly when
- 00:19:46 you need them you can switch to a
- 00:19:48 database with the use command
- 00:19:50 for example we can use first
- 00:19:53 test here now we switch to the first
- 00:19:55 test database even though that didn't
- 00:19:58 exist up there
- 00:19:59 if we now type show dbs we also still
- 00:20:01 see it because it's empty
- 00:20:03 but since we switched to first test here
- 00:20:06 we can actually now use it with the db
- 00:20:09 command
- 00:20:10 and we can chain commands to that and
- 00:20:13 now we are on that collection level
- 00:20:15 right we're in the database if you
- 00:20:17 remember that
- 00:20:18 slide from earlier we now work with
- 00:20:20 collections which then hold documents
- 00:20:22 now let's say we want to manage a couple
- 00:20:24 of users
- 00:20:25 we can simply add such a collection also
- 00:20:28 on the fly there is no special create
- 00:20:30 collection command
- 00:20:32 with db dot users if you worked with
- 00:20:35 javascript or actually with most
- 00:20:36 programming languages
- 00:20:38 you know that dot notation from objects
- 00:20:40 and it's pretty similar here mongodb
- 00:20:42 which is why it's really fun to use it
- 00:20:44 as a developer a lot of the way you work
- 00:20:47 with that database
- 00:20:48 resembles the way you write code so now
- 00:20:51 i'm accessing the user's collection
- 00:20:53 which does not exist yet
- 00:20:55 in the first test database which also
- 00:20:57 doesn't exist yet
- 00:20:58 and now i want to enter a user and that
- 00:21:01 is the part where then both the database
- 00:21:03 and the collection will be created once
- 00:21:05 i really start storing data
- 00:21:08 now to store data i can run insert and
- 00:21:11 now we have insert many
- 00:21:13 or insert one by the way you have auto
- 00:21:15 completion with tab
- 00:21:17 insert one as the name suggests inserts
- 00:21:20 one
- 00:21:20 element into that collection in that
- 00:21:22 database and now that element is uh like
- 00:21:25 a json object we
- 00:21:26 use curly braces here to define that
- 00:21:29 object
- 00:21:30 now unlike in json you now don't need to
- 00:21:32 wrap your keys and double quotation
- 00:21:34 marks
- 00:21:34 you can instead just write name colon
- 00:21:37 and then the value
- 00:21:38 so name is the the key of that
- 00:21:42 entry you're adding to the database and
- 00:21:44 now the value here is max
- 00:21:46 now these single quotation marks are
- 00:21:48 only needed because max should be a
- 00:21:50 string you're not limited to strings of
- 00:21:52 course
- 00:21:52 you can store booleans numbers you can
- 00:21:55 even store
- 00:21:56 files in there though that is typically
- 00:21:58 not what you want to do
- 00:21:59 you rather want to store files on a file
- 00:22:01 system and store the path to the files
- 00:22:03 in the databases but that's just a side
- 00:22:05 note
- 00:22:06 and let's also add a number field
- 00:22:08 therefore separated by a comma
- 00:22:10 my age if i now hit enter this
- 00:22:13 created that object and it automatically
- 00:22:16 assigned an id which you can see here
- 00:22:18 itself another type a so-called object
- 00:22:20 id type
- 00:22:21 but in the end this is the id which was
- 00:22:23 stored now it would be nice to also see
- 00:22:26 that entry
- 00:22:27 first of all let's again type show dbs
- 00:22:29 and now you see that first
- 00:22:31 test database which was added because we
- 00:22:33 now have a collection in there
- 00:22:35 you can also see that if you type show
- 00:22:37 collections now here you see that user's
- 00:22:39 collection which we created on the fly
- 00:22:41 and in that collection if you want to
- 00:22:43 see all you can just access db.users
- 00:22:45 which is the collection
- 00:22:46 and then find if you execute it like
- 00:22:49 this it finds
- 00:22:50 all the documents in there and therefore
- 00:22:52 right now we only see
- 00:22:54 one and that is the one document we
- 00:22:55 created with our name and our age
- 00:22:58 here you now see these double quotation
- 00:23:00 marks around the keys to
- 00:23:03 and also that automatically generated id
- 00:23:06 so this is how we can add and find
- 00:23:09 documents now often we of course
- 00:23:12 want to find um just a subset
- 00:23:16 of all the documents now for that let's
- 00:23:17 first of all add more
- 00:23:19 let's first of all clear the console so
- 00:23:21 that it's easier to read with the cls
- 00:23:22 command
- 00:23:23 we're still in the database we're still
- 00:23:25 connected i just emptied the screen
- 00:23:27 and now let's simply add more and for
- 00:23:29 that i'll insert
- 00:23:31 many users now insert many simply takes
- 00:23:35 um
- 00:23:35 as many entries as we want so we can add
- 00:23:38 a user here
- 00:23:39 let's say name man u
- 00:23:42 l h or let's leave out the age here to
- 00:23:46 show that versatility
- 00:23:48 let's add another document here an
- 00:23:51 arbitrary
- 00:23:52 person which i don't know chris
- 00:23:55 h could be 25. and let's hit enter
- 00:23:59 and now i get an error here the reason
- 00:24:02 for that is
- 00:24:03 that this insert many command here
- 00:24:05 actually doesn't take all the documents
- 00:24:07 separated by a comma like this
- 00:24:09 instead we have to pass an array here so
- 00:24:12 squared brackets
- 00:24:14 let's add them here around our documents
- 00:24:17 we want to add
- 00:24:18 and now this succeeded now if i type db
- 00:24:21 users find
- 00:24:22 we see all three entries we see that
- 00:24:26 manual is missing an
- 00:24:27 agent still was added without issues to
- 00:24:29 that collection
- 00:24:30 now let's say we want to add one more
- 00:24:32 user the users
- 00:24:33 insert 1 and that user here
- 00:24:37 will have a name which is
- 00:24:41 lisa and the age is
- 00:24:44 26. so now we have if we
- 00:24:48 type db users find we have these four
- 00:24:51 entries or these four documents in there
- 00:24:54 now let's say we want to
- 00:24:55 find all the users with an age greater
- 00:24:58 than 25 so that chris is actually not
- 00:25:01 included
- 00:25:02 well for that we can access this again
- 00:25:05 and
- 00:25:05 write find again but now don't hit enter
- 00:25:08 find
- 00:25:09 as pretty much all these query commands
- 00:25:12 you can execute
- 00:25:13 takes optional arguments and here we can
- 00:25:16 pass a so-called filter
- 00:25:17 we again pass curly braces but now we're
- 00:25:20 not defining which element to enter
- 00:25:22 but our filter criteria we could for
- 00:25:25 example type
- 00:25:26 h 26 to find all entries where the age
- 00:25:28 is 26.
- 00:25:30 and therefore we only find lisa because
- 00:25:32 she's the only
- 00:25:33 person in or the only document in that
- 00:25:35 collection with an age of 26
- 00:25:37 but i wanted to find all persons with
- 00:25:41 an h greater than 25 and for that we can
- 00:25:44 use the so-called query criteria i add
- 00:25:47 another set of curly braces
- 00:25:49 as a value for the age here and in there
- 00:25:51 there are a couple of reserved
- 00:25:53 operations or
- 00:25:54 helpers we can use and you can find them
- 00:25:56 all in the official box
- 00:25:58 these all start with a dollar sign and
- 00:26:00 then there is the gt
- 00:26:02 the greater than helper and now i can
- 00:26:05 define the value
- 00:26:06 which is the value i want to have or i
- 00:26:09 want to find records which have a
- 00:26:11 greater than
- 00:26:12 value so if i enter 25 here now i find
- 00:26:15 lisa and max but not
- 00:26:16 chris because 25 is not greater than 25
- 00:26:19 obviously
- 00:26:20 and this shows you how easy and fun
- 00:26:24 it is to work with mongodb and write
- 00:26:27 more complex queries it's not super
- 00:26:29 complex to be honest
- 00:26:30 but it is a good start now let's add
- 00:26:34 some nested record for that i'm clearing
- 00:26:37 the window again
- 00:26:38 i'll again reach out to my users and
- 00:26:40 insert
- 00:26:41 one user and that one user
- 00:26:44 will have a name let's say susanna
- 00:26:48 will have age let's say 30 and we'll
- 00:26:52 have an address which then simply is
- 00:26:54 another document
- 00:26:56 created with curly braces here and that
- 00:26:59 will have a street
- 00:27:02 first first street
- 00:27:06 and then let's say a city um let's say
- 00:27:09 that is
- 00:27:10 new orleans i don't know if that exists
- 00:27:12 that address by the way
- 00:27:14 let's hit enter if we now
- 00:27:17 find all users again we see susanna
- 00:27:19 there with the nested address
- 00:27:22 now we can also run queries on such
- 00:27:24 embedded documents
- 00:27:25 for that we can again find and now let's
- 00:27:29 find all users
- 00:27:33 where the address street is first street
- 00:27:36 we only have one such record we only
- 00:27:38 have one record with an address anyways
- 00:27:40 but still i just want to show you how
- 00:27:41 that works for that we again add
- 00:27:44 curly braces to define our search credit
- 00:27:47 criteria our filter here
- 00:27:49 and now in there we need to use double
- 00:27:52 quotation marks
- 00:27:53 and then address which is the field in
- 00:27:56 the document in the user document itself
- 00:27:59 which then holds the nested document as
- 00:28:01 a value
- 00:28:02 then we use dot notation in here which
- 00:28:04 is why we need to double quotation marks
- 00:28:06 and then the nested field so the street
- 00:28:08 and then again
- 00:28:09 just the value like first
- 00:28:13 street here if we now hit enter we find
- 00:28:16 that one entry that matches this
- 00:28:17 and if i type something else like here
- 00:28:20 have an
- 00:28:21 error in the street we don't find
- 00:28:23 anything of course
- 00:28:24 so this is how we can insert and find
- 00:28:27 our documents
- 00:28:28 now obviously a common task also is to
- 00:28:32 update or delete them
- 00:28:33 now let's start with updating for that
- 00:28:35 we have a number command
- 00:28:36 the update command and for that command
- 00:28:40 here
- 00:28:41 we basically need two arguments the
- 00:28:44 first one
- 00:28:44 identifies which document to update
- 00:28:48 the second one defines how to update it
- 00:28:51 and then there's a third one where you
- 00:28:53 can also configure the update mechanism
- 00:28:55 for example define what should happen if
- 00:28:57 no document is found should it create
- 00:28:59 one or do nothing
- 00:29:00 or if multiple documents are found for
- 00:29:02 your filter should it update them all or
- 00:29:04 just the first one
- 00:29:06 but we'll focus on the first two
- 00:29:07 arguments only the first one identifies
- 00:29:10 which document to update and let's say
- 00:29:12 we want to update
- 00:29:14 the user max so we want to update the
- 00:29:16 document where name is equal
- 00:29:18 to max and then the second value here
- 00:29:21 for the update command
- 00:29:23 is how do we want to update it and now
- 00:29:25 there's one important thing
- 00:29:27 we can update in two different ways the
- 00:29:29 first one
- 00:29:30 is that we use curly braces and then
- 00:29:33 dollar
- 00:29:33 sign set and then we define in another
- 00:29:37 set of curly braces
- 00:29:38 which field to change in which way for
- 00:29:40 example if i change h to 30 here
- 00:29:43 and then i do db users find
- 00:29:46 we see that for max the age was changed
- 00:29:49 to 30
- 00:29:50 but please note that the name was kept
- 00:29:53 if i repeat that command without dollar
- 00:29:56 sign set
- 00:29:57 so if my whole second argument is just
- 00:29:59 age 30
- 00:30:01 then you will notice that it still
- 00:30:03 updates but if i find
- 00:30:04 all users the name is gone please note
- 00:30:07 that the id was not
- 00:30:09 changed the two ids here are equal but
- 00:30:11 the name is gone
- 00:30:13 so the update method actually differs
- 00:30:16 if we use dollar sign set we do a
- 00:30:19 patch update which means we only change
- 00:30:22 the fields we defined to set
- 00:30:25 if we don't use dollar sign set but we
- 00:30:27 just pass a new
- 00:30:28 object as a value this new object will
- 00:30:31 replace the old one but we'll keep that
- 00:30:33 old id
- 00:30:34 so it's still the same document but all
- 00:30:37 the values have been
- 00:30:38 replaced and values which were included
- 00:30:41 previously
- 00:30:42 but are gone now will simply be dropped
- 00:30:45 so this is the update command and
- 00:30:47 finally if you want to delete something
- 00:30:50 well then we can use db
- 00:30:53 users delete and then there is delete
- 00:30:56 many
- 00:30:56 or delete one now delete one is
- 00:30:59 straightforward
- 00:31:00 you again have to search for what you
- 00:31:02 want to delete and that works in the
- 00:31:03 same way as it does in update and as it
- 00:31:05 doesn't find
- 00:31:07 by the way there also would have been
- 00:31:08 find one and so on
- 00:31:10 if you just want to find one element
- 00:31:12 instead of all matching
- 00:31:13 but that's just a side note let's go
- 00:31:15 back to delete one
- 00:31:17 so let's say we want to delete the
- 00:31:20 user with the name chris if i hit enter
- 00:31:23 here
- 00:31:23 you see it deleted one so this seemed to
- 00:31:26 work
- 00:31:27 and if we now find indeed we only have
- 00:31:29 four records instead of five
- 00:31:31 and chris is gone now of course there
- 00:31:33 are other commands and a lot of ways of
- 00:31:36 using all these commands
- 00:31:37 and i strongly recommend diving into the
- 00:31:39 official docs and just play around with
- 00:31:41 them
- 00:31:42 as you see it's really easy to play
- 00:31:43 around and you can't break anything it's
- 00:31:45 just a
- 00:31:46 sandbox database we're using here
- 00:31:48 anyways
- 00:31:50 so this is how we can play around with
- 00:31:52 it in the console
- 00:31:53 but of course sometimes it's also nice
- 00:31:55 to have a graphical user interface
- 00:31:57 and we can get that too with mongodb
- 00:32:00 compass
- 00:32:00 for that let's connect again and let's
- 00:32:02 choose connect with mongodb compass
- 00:32:05 choose your operating system and
- 00:32:07 download it because this is a tool you
- 00:32:08 install locally
- 00:32:10 now as you can see it's quite big
- 00:32:12 because it's a well
- 00:32:13 full uh program with a graphical user
- 00:32:16 interface and a lot of logic attached to
- 00:32:18 it
- 00:32:18 so let's wait for this to finish and
- 00:32:19 then simply walk through the installer
- 00:32:22 and we'll be able to then reach out to
- 00:32:25 our cluster
- 00:32:26 and inspect it with that interface in
- 00:32:28 just a second
- 00:32:29 so once you installed it and there
- 00:32:31 should be a straightforward installation
- 00:32:33 process for your given operating system
- 00:32:36 you can connect to it now you'll
- 00:32:37 probably use the latest version if you
- 00:32:39 just installed it so you can copy that
- 00:32:41 url to connect to your database so let's
- 00:32:45 copy here
- 00:32:45 you'll need your password here as it of
- 00:32:48 course says
- 00:32:49 and if you copied that string from the
- 00:32:51 previous page it actually should ask
- 00:32:53 automatically if it should use that
- 00:32:55 though i also need to agree on the
- 00:32:57 license agreements
- 00:32:58 yes this is how it works thanks very
- 00:33:00 much and it should now have populated
- 00:33:03 that with some basic information
- 00:33:05 if not you can basically
- 00:33:08 edit manually now in here choose your
- 00:33:11 username
- 00:33:13 the password which you have set up on
- 00:33:15 the mongodb cluster
- 00:33:18 leave that authentication database here
- 00:33:21 because that is the authentication
- 00:33:23 database you have
- 00:33:24 created the user on typically now if
- 00:33:27 that fails
- 00:33:28 also try creating a new user and make it
- 00:33:31 an atlas
- 00:33:32 admin and with that added
- 00:33:36 you should be able to connect now once
- 00:33:39 you're connected you see
- 00:33:40 all the databases running on this
- 00:33:42 cluster and therefore we also see the
- 00:33:44 first
- 00:33:44 test database and if you click on that
- 00:33:47 you see all the collections
- 00:33:48 also here on the left and if you click
- 00:33:50 on a connection you of course
- 00:33:52 also see all the documents so here are
- 00:33:55 documents we added
- 00:33:56 with the nest data and if you want to
- 00:33:59 insert a document here
- 00:34:01 we can do that too so we can also now
- 00:34:04 edit all the data in here we can also
- 00:34:07 create new collections in here
- 00:34:09 as you can see we could add our products
- 00:34:12 collection
- 00:34:13 and that allows us to work with this in
- 00:34:15 a nice and easy way though it is super
- 00:34:17 important to know all these commands you
- 00:34:19 saw
- 00:34:20 in the terminal in the shell
- 00:34:23 because these are the commands you will
- 00:34:25 also write in your applications which
- 00:34:27 you then connect
- 00:34:28 to mongodb so that is mongodb in a
- 00:34:32 nutshell how we work with it how we
- 00:34:34 manipulate data
- 00:34:35 and how it thinks and works and in my
- 00:34:37 opinion it's
- 00:34:38 really easy to work with it as a
- 00:34:40 developer it's so
- 00:34:42 close to how you write code in my
- 00:34:44 opinion much easier to work with it
- 00:34:46 than with sql databases though of course
- 00:34:48 that's just
- 00:34:49 my opinion now mongodb is actually
- 00:34:53 more than just the database though
- 00:34:56 or even for the database there's more
- 00:34:58 than one option
- 00:34:59 and that is what i want to conclude this
- 00:35:01 video with
- 00:35:02 if we are on mongodb.com we just got
- 00:35:05 started
- 00:35:06 or downloaded mongodb but if you click
- 00:35:09 on solutions
- 00:35:10 you actually see they got a couple of
- 00:35:12 other products too now what's that all
- 00:35:14 about mongodb enterprise advanced and
- 00:35:18 for oem are basically also just the
- 00:35:20 database the database servers
- 00:35:22 for you to install on your own servers
- 00:35:26 and to work with them so not the cloud
- 00:35:28 managed version
- 00:35:29 just a commercial self-managed database
- 00:35:33 and ops manager and cloud manager are
- 00:35:35 connected to that
- 00:35:36 because these are tools which allow you
- 00:35:39 to manage
- 00:35:40 your database manage your own shards
- 00:35:43 configure it secure it
- 00:35:45 all that stuff you have to do if you're
- 00:35:47 running your own databases
- 00:35:48 and which then is made a bit easier but
- 00:35:50 still database administration
- 00:35:53 is something we developers probably
- 00:35:55 won't do
- 00:35:56 because it is easy to mess up and
- 00:35:59 something you probably should get right
- 00:36:02 that is why we have mongodb
- 00:36:04 atlas which makes that a lot easier and
- 00:36:06 which for example allows you if you get
- 00:36:08 a great app idea
- 00:36:09 to focus on your code and don't worry so
- 00:36:11 much about the database
- 00:36:13 now we also got other options here
- 00:36:15 though we saw compass
- 00:36:17 that is our graphical ui but what is
- 00:36:20 stitch
- 00:36:22 mongodb stitch is a pretty cool offering
- 00:36:26 by mongodb which is only
- 00:36:28 loosely connected to their database
- 00:36:30 stitches you could say
- 00:36:32 their serverless services offering
- 00:36:35 now if that's not telling you anything
- 00:36:37 serverless basically does not mean that
- 00:36:39 there are no servers involved
- 00:36:41 instead these are services which are all
- 00:36:43 around
- 00:36:44 you don't need to manage any servers not
- 00:36:46 only database servers but also
- 00:36:48 application servers
- 00:36:50 you can focus on your code to be more
- 00:36:52 concrete stitch gives you a couple of
- 00:36:56 features which allows you for example to
- 00:36:59 query the database from anywhere
- 00:37:01 including angular or react apps and it
- 00:37:04 does so
- 00:37:05 by in the end giving you some hidden
- 00:37:08 api which you normally have to write on
- 00:37:11 your own with node with php whatever it
- 00:37:13 is
- 00:37:14 it gives you that and simply exposes you
- 00:37:17 some
- 00:37:18 functionalities you can execute safely
- 00:37:20 in your client-side code where
- 00:37:22 people are not able to get your
- 00:37:24 credentials and start working with your
- 00:37:26 database all of a sudden
- 00:37:27 instead in the end you get an invisible
- 00:37:29 api you could say
- 00:37:31 stitch functions is a serverless
- 00:37:34 offering which is completely decoupled
- 00:37:36 from the database
- 00:37:37 here this is basically something like
- 00:37:39 aws lambda or firebase cloud functions
- 00:37:42 so it's code in the cloud you write some
- 00:37:45 code
- 00:37:45 um which you then just upload there
- 00:37:48 which you can
- 00:37:49 execute which you can trigger and which
- 00:37:51 does something
- 00:37:52 something that can interact with the
- 00:37:54 mongo database but doesn't have to
- 00:37:56 necessarily
- 00:37:58 stitch triggers then works great
- 00:38:00 together with stitch
- 00:38:01 functions because stitch triggers allows
- 00:38:04 you to
- 00:38:05 run your functions in response to
- 00:38:07 certain database events
- 00:38:09 let's say you have a database where you
- 00:38:11 manage user signups
- 00:38:13 when a user signs up you maybe want to
- 00:38:15 send an email to that user
- 00:38:17 and you could use stitch triggers to
- 00:38:19 listen to changes to your user database
- 00:38:22 and whenever or to your user collection
- 00:38:24 let's say
- 00:38:25 and whenever a new document is added so
- 00:38:27 whenever a user is signed up
- 00:38:29 you send an email to the email address
- 00:38:31 which probably is stored in that
- 00:38:33 document and you do so by using a stitch
- 00:38:36 trigger
- 00:38:36 which runs a stitch function which then
- 00:38:39 in turn does
- 00:38:40 send that email through some third-party
- 00:38:42 service
- 00:38:44 stitch mobile sync is something which is
- 00:38:47 coming soon and which is related to
- 00:38:48 another solution
- 00:38:50 mongodb mobile mongodb mobile is mongodb
- 00:38:54 running on a mobile device and the idea
- 00:38:57 behind that of course is
- 00:38:59 that connectivity always is kind of an
- 00:39:01 issue on mobile devices
- 00:39:03 maybe your users don't have an internet
- 00:39:05 connection
- 00:39:06 maybe they only have a slow one well if
- 00:39:09 you have the database on the device
- 00:39:12 then your users can interact with your
- 00:39:14 app to some extent
- 00:39:16 even without an internet connection they
- 00:39:18 can for example create new posts and you
- 00:39:20 store them locally
- 00:39:21 and then once the connection is back up
- 00:39:23 you sync them to the cloud
- 00:39:25 and mongodb stitch sync is a service
- 00:39:28 that makes that syncing process
- 00:39:29 fully automated and easier which will be
- 00:39:32 available soon
- 00:39:33 so that allows you to write apps that
- 00:39:36 work great on mobile devices or
- 00:39:38 anywhere and where you can sync your
- 00:39:40 data around and therefore hopefully
- 00:39:42 provide a great user experience
- 00:39:44 mongodb charts is another offering where
- 00:39:47 you can easily connect
- 00:39:49 charts to your database
- 00:39:52 so you have a web interface where you
- 00:39:54 can create donut or
- 00:39:56 pie charts or line charts bar charts
- 00:39:59 on your mongo database data without you
- 00:40:02 writing any code
- 00:40:04 and you also got some connectors which
- 00:40:06 allow you to connect other
- 00:40:08 business intelligence tools like tableau
- 00:40:10 to your mongodb
- 00:40:12 data so that is the rich
- 00:40:15 suite of things mongodb offers
- 00:40:18 the database is their core offering and
- 00:40:21 almost all our services are connected to
- 00:40:24 it in some way
- 00:40:25 but in general mongodb also is a bit of
- 00:40:27 a broader platform
- 00:40:29 offering or with the goal of offering
- 00:40:32 you everything you need
- 00:40:33 to build modern web or mobile or desktop
- 00:40:37 applications with a strong and very fast
- 00:40:39 and fun to work with database as you saw
- 00:40:42 and a couple of other things that
- 00:40:43 hopefully make your life easier
- 00:40:45 so that's mongodb in a nutshell i hope
- 00:40:48 this was helpful
- 00:40:49 and definitely let me know if you like
- 00:40:51 this if you want to see more
- 00:40:53 we can do videos on how we work with
- 00:40:55 mongodb
- 00:40:56 from for example node.js and so on and
- 00:40:59 of course i also get a couple of