Learn MongoDB in 1 Hour πŸƒ (2023)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's it going everybody it's you bro hope you're doing well and in today's video I'm going to teach you everything you need to know to get started using mongodb in about an hour so sit back relax and enjoy the show hey if you'd like free stuff you should probably subscribe because I release free courses every once in a while when I feel like it that is all what the heck is mongodb it's a no SQL database management system that can manage a humongous amount of data and it's becoming increasingly popular it's favored by many big tech companies unlike relational database Management Systems mongodb uses a nosql format to store and retrieve data nosql means not only SQL it's where data is stored in various formats besides a traditional SQL table rather than storing our data in rows and columns with the table using SQL we store related data as a single document think of each document as a single row and a table in SQL data in each document is stored as field value pairs similar to a Json format but it's technically bsawn binary JavaScript object notation but it behaves very similar similarly for all intents and purposes the general idea is that data which is frequently accessed together is stored together rather than in Separate Tables because SQL joins quite frankly are a pain in the ass this is how our nosql database is arranged we have a document a document is a group of field value pairs to represent an object a collection is a group of one or more documents and a database is a group of one or more collections this makes working with and scaling our database like really really easy before we do begin there are a few prerequisites for this series we will be working with document objects I would recommend at least some experience with an object-oriented programming language of your choosing it doesn't matter if it's python Java C plus plus C sharp JavaScript doesn't matter just pick one so let's get started I will show you what you need to download all right let's get started everybody here is the installation process for mongodb so go to this website mongodb.com go to resources server on the left sidebar go to installation here's a list of different installation tutorials depending on your operating system I'm running Windows I'll select Windows let's scroll down we're looking for install mongodb Community Edition all you have to do is follow the numbered steps beginning with one we will go to the download Center I'll open this in a new tab because I don't want to close out of this window select the current version select your platform again I'm running Windows and the installation package I'll keep that the same and download pick a place to save the installer to I'll keep it within my documents folder but you choose your own place and I will open this when it's done so we're going to click next accept the license agreement next complete install mongod as a service make sure that's checked next also be sure to install the mongodb compass program it's a graphical user interface then give it a couple minutes now that mongodb is installed we can finish this may open compass compass is a graphical user interface for managing our database we'll be using Compass intermittently in fact I recommend creating a desktop shortcut you just have to open it then to establish a connection you hit the green connect button I'll be using Compass intermittently just to show you how everything works let's also install the mongodb shell to install the shell we're going to go to the left sidebar and where is it there mongodb shell also known as mangosh like oh my gosh we are going to go to the download Center select the current version select your platform I'm running Windows and the package will keep that the same and download we'll open this when it's done we will unzip this folder so right click extract all we will need the file path to the executable let's open this folder go to bin and we can copy this file location let's right click on our executable go to properties copy this file location it should be within the bin folder then we're going to add that file path to our environment variables really we can just search for environment variables we're within system properties go to the advanced tab environment variables underneath system variables let's pretend that this wasn't here click new come up with a variable name sh is good then paste that file location hit okay and close so if we go to that folder and open the executable this is the mongodb shell I'm going to make the font size a little bit bigger to establish a connection to our database type sh and that should establish a connection to clear your screen type CLS to exit type exit personally I don't like using this executable by itself I like using vs code if you would like to use vs code for using the Shell here's how if you would like to use vs code for using the Shell here's how So within vs code we're going to download the mongodb extension so go to extensions search for mongodb it should be this one we will install this extension there should be a new icon for mongodb let's click on it to establish a connection we just have to click on localhost the mongodb connection was successful then to open our terminal right click launch mongodb shell let's minimize this close out of this window pull up this terminal we are utilizing the mongodb Shell with vs code so to exit you type exit to establish a connection you can type sh to clear your screen you can type CLS and that is everything you need to know to get started everybody here's how we can create and use databases in mongodb to show all databases type show DBS that will give you a list of all current databases I have three admin config and local to use a database type use then select a database name to use admin I can say use admin we have switched to dbadmin now if you use a database that doesn't exist let's say we will create a database for a school use school that's how you can create a database we are now within a new database named School however if I were to show my current databases my school database doesn't show up it's not visible because it's empty we'll need to fill it with something let's add a collection to our school to do that type DB dot we will use the create collection method this method ends with a set of parentheses just like a function or method in typical programming languages within the set of parentheses we are going to pass in an argument for a name for this collection let's create a collection of students and we are returned with OK we have created one new collection let's clear our screen show our databases again show DBS and here is our new database of school currently we are within our school database as you can see next to my cursor to drop this current database you can type DB dot drop database method and our database is dropped let's show our databases again and our school database is no longer there that is how to use create and drop a database in the mongodb Shell if you would like to use Compass here's how we have our three default databases admin config local to create a database you can click this green button we'll need a database name let's say school again we will create a collection of students within our database you can add additional preferences but we'll cover that later we will create our database and here's our database of school there is one collection of students but students doesn't have any documents yet which we'll cover in the next topic to delete a database in mongodb compass select a database from the sidebar then hit the trash can icon to delete it type the name of the database to drop the database so make sure that the name of the database is typed then drop database and that is how to use create and drop a database in mongodb compass all right we're moving on everybody now I'm gonna show you how we can insert documents into a mongodb database let's be sure that we're using our current database in the last topic we were using the database of school let's clear our screen to insert a document within the database we're working with type DB then the name of a collection if a collection is found within this database we have created a collection of students if this collection doesn't exist one will be created for you db.students.we will use the insert one method within the set of parentheses we will place a document which is enclosed within a set of curly braces within the set of curly braces we can list as many field value pairs as we would like let's create a name field with the value of SpongeBob each field value pair is comma separated let's add another field value pair maybe an age age will be 30. then a GPA grade point average SpongeBob has a solid 3.2 GPA let's enter this command that was acknowledged to return all documents within a collection type DB the name of the collection in this case students dot we can use the find method here are the current documents within my collection we only have one here's the object ID that's created automatically the name is SpongeBob age 30 GPA 3.2 now you can insert more than one document at a time by using the insert menu function DB type the name of the collection students in this case we will insert many documents we will place all documents to be inserted within an array add a set of straight brackets this is the exact same kind of array from different programming languages however in Python I think that would be a list technically maybe we need to insert three documents we would need three sets of curly braces each comma separated within each set of curly braces we would list all of the different field value pairs so let's begin with the first we have name Patrick separate each field value pair with a comma then we have age of let's make Patrick 38 then GPA Patrick is at 1.5 that's enough for our first document let's go to the second document move over to the second set of curly braces for the name this will be Sandy Sandy's age is I don't know 27 sounds good GPA Sandy smart she has a solid 4.0 then let's go to the last document name maybe Gary 's age is 18. GPA 2.5 that's good enough for now this array contains several documents each document is enclosed within a set of curly braces within each document you can have as many field value pairs as you like they all don't need to be consistent let's insert all of these documents they were all acknowledged we have three new object IDs let's use the find method again to display all of them DB dot students dot find here are the four objects found within our database SpongeBob Patrick Sandy Gary that's how to insert documents using the Shell next we'll do the same thing but use Compass instead if you're using Compass here's how to insert documents into a collection here are the four documents we created previously I'm just going to go ahead and delete these to add a new document make sure you're within the current database you're working with go to the current collection RS students we will add data you can import a Json or CSV file or you can type them manually by inserting document a document is pre-generated for you I'm just going to go ahead and delete that we'll Begin by inserting SpongeBob you need a set of curly braces then list all of the name value pairs name SpongeBob separate each field value pair with a comma age 30. GPA 3.2 if you have this warning you will need to probably hit the format button that should go away then insert and that is how to insert one document to insert many it's the same process as before but you're going to enclose all of your documents within an array a set of straight brackets so if we need three documents add three sets of curly braces all comma separated we had Patrick age I think he was 38. GPA 1.5 name Sandy age 27 I'm going to move this down a little bit so you can see GPA 4.0 then name Gary age 18. GPA 2.5 let's reformat insert and here are the three documents we have inserted all right and that is how to insert documents into a mongodb database using Compass all right everybody it's about time I discuss some basic data types in mongodb we will Begin by inserting a new document into our students collection db.students.insert1 we're inserting a document add a set of curly braces the first data type I'll discuss is a string suppose we have a field of name a string is a series of text within quotes these can be either double quotes or single quotes but typically I like to put them within double quotes a string is a series of text like a name Like Larry strings can contain spaces we could enter in a first name space then a last name Larry Lobster they can also contain numbers larry123 however in a string we treat the numbers differently we read the Morris characters rather than actual numbers so let's stick with name Larry that's a string it's a series of characters then we have integers and integer is a whole number let's create a field for age Larry will be 32. whole integers don't contain decimal portions it wouldn't make sense to enter in 32.5 we don't really keep track of people's half birthdays and stuff so age would be a whole number those are integers whole numbers then we have doubles a double is a number that contains a decimal portion maybe a grade point average grade point averages typically contain decimals Larry has a 2.8 GPA it's a double because there's a decimal portion integers are whole numbers doubles are numbers that contain a decimal then we have booleans booleans are either true or they're false it's kind of like a light switch it's only on or off there's only two states let's create a field for full time is the student full time this can be either true or false Larry is too busy working out all the time he is a part-time student not a full-time student we'll set that field to be false date objects are pretty popular in mongodb suppose we have a registration date register date to create a date object you can use the new keyword followed by a call to the date Constructor if you don't pass any arguments to the date Constructor you'll use the current time in the UTC time zone otherwise you can pass a date and time like 2023 January 2nd you could add a time too but we'll just stick with the current date and time that's assigned to us those are date objects the next data type is null null means no value I'll create a field for a graduation date and I will set that to be null Larry is a current student but we don't know when he's going to graduate all we're really doing is creating a placeholder we don't have a date yet so we can set that to be null that's to be filled in later so null means no value then we have arrays much like in modern programming languages is kind of like a variable that has more than one value but in mongodb we have a field that can have more than one value let's create a field of courses what courses is Larry enrolled in place all values within a set of straight brackets let's insert multiple values what courses is Larry in maybe he's in biology these values are all comma separated chemistry then calculus so that's an array they're all enclosed within a set of square brackets they allow one field to contain multiple values then we have nested documents this would be good for an address to create a nested document you use a set of curly braces within the nested document we can list some field value pairs how about a street let's come up with a street name one two three fake street is good than a city Bikini Bottom and a zip code one two three four five it is possible to have documents within documents which is what we did with the address let's enter everything to see if it works yeah that was acknowledged let's head to Compass refresh and here is Larry name Larry h32 GPA 2.8 full time is set to false he's a part-time student here is Larry's registration date and time we're using the UTC time zone his graduation date is set to null all of his courses that he's in is stored within an array and his address is stored as a nested document alright everybody and those are a few basic data types you should know in mongodb all right everybody today I'm going to show you how we can both sort and limit documents in mongodb to return all documents we can type DB the name of the collection dot then we can use the find method to return all documents to sort these documents in some sort of order again we would type DB the name of the collection students dot find method then we'll do some method chaining after the find method add dot then we will call the sort method the sort method takes a document by which field would we like to sort let's sort names in alphabetical order we would type the name of the field Name colon then one for alphabetical order or negative one for reverse alphabetical order so if we were to sort by alphabetical order here's the results we have Gary then Larry Patrick Sandy SpongeBob then reverse alphabetical order that was Name colon negative one now we have SpongeBob Sandy Patrick Larry Gary let's sort our documents by GPA grade point average so again we use the find method then we'll method chain the sort method we're passing in a document object with our criteria we will sort by GPA in ascending order so we will use one I'm going to copy this because I'm going to use it later okay now we're sorting by GPA Patrick has the lowest GPA 1.5 2.5 2.8 3.2 then 4. to sort our GPA by deciding order we would type GPA negative one then we have Sandy at the top with the gpf4 then SpongeBob with 3.2 2.8 2.5 1.5 now we have the limit method we can limit the amount of documents that are returned to us let's type DB dot students dot find method dot limit method we will pass the number of documents we would like return to us as an argument if I would like one document returned to us I will pass in one currently my documents are sorted by object ID this would give us the first document of SpongeBob if I were to use limit 3 we would be returned three documents SpongeBob Patrick Sandy we can combine both the sort and the limit methods too I would like to return the student with the highest GPA we'll use the find method then method chain the sort method we are sorting by GPA we're going to sort in decreasing order so negative one followed by the limit method I would like just one document who has the highest GPA that would be Sandy with a GPA of four who has the lowest GPA we should sort an increasing order that would be one then limit one so Patrick has the lowest GPA of 1.5 now let's work with sorting and limiting Encompass to sort the documents in your collection you can press more options then add some sort criteria I will sort by name colon one then find so we have Gary Larry Patrick Sandy SpongeBob then negative one for reverse alphabetical order SpongeBob Sandy Patrick Larry Gary let's sort by GPA GPA negative one that would give us decreasing order so Sandy's at the top to limit the amount of documents that are returned you can enter in a number for the limit box I will limit the records to one so we are returned with Sandy she has the highest GPA limit three would give us the top three students or leave limit MDU to return all documents all right everybody so that is basic sorting and limiting in mongodb hey everybody in this topic I'm going to explain the find method in mongodb to use the find a method type DB the name of the collection you're working with then add the find method that will return all documents within this collection but at times you might not want all documents you might want specific documents here's how we will add some arguments to the find method DB Dot students.find the first argument is a document object the parameter is the query parameter it specifies selection filters maybe you just need one student well you would just list that criteria here I would like any students with the name of SpongeBob that would return any document where the first name is SpongeBob that would return everybody that has that name I'm just going to copy this let's find any students that have a GPA of 4.0 within our document the search filter is GPA 4.0 that would give us Sandy let's find if any students are not full-time full time I will set that to be false there is one student Larry full time is set to false the query parameter is very similar to a where clause in SQL you can even use more than one filter search for any student that has a GPA of 4.0 each filter is comma separated as well as full-time being true with my data set this does not return any results but to add more than one filter they're comma separated within a document body with the find method there is a second parameter that is the projection parameter we would pass another document body they're each comma separated if you don't specify the query parameter you would just return all documents with the projection parameter you can return specific Fields maybe you don't want all the information that's available perhaps I would just like each Student's name so we would set the field to be true you can use one as well but my own preference is true just because it's more readable return every document but only give their name and here they are mongodb will give you the ID regardless you can set that to be false within the projection parameter underscore ID false I just want everybody's name there we are SpongeBob Patrick Sandy Gary Larry let's get every student's name as well as their GPA so ID I will set that to be false I don't want that name will be true I would like that GPA will also be true I would like that here is every student's name as well as their GPA so that's the projection parameter if there's a field you would like or don't want you can set that to be either true or false when you display your data all right let's do the same thing with compass with compass this text box is for the find method So within a set of curly braces I would like to find any name that's SpongeBob then press find that returns one result return any student with the GPA of 4.0 that's Sandy return any students that are not full-time full-time is false that gives us Larry now if you go to more options there is a projection parameter if you only want some data let's find every student but for the projection parameter I would only like their name I'll set that to be true and their GPA that's true as well again that still gives us the ID let's set that to be false if you don't want that ID false and that should get rid of the ideas here's each Student's name and their GPA all right everybody that's the find method two optional parameters that we can send arguments to are the query parameter that's selection criteria it's similar to a where clause in SQL we're trying to limit the results and the projection parameter that's similar to selected columns in SQL you're looking for specific Fields alright everybody and that is the find method in mongodb well hello again everybody today I will be explaining how we can update documents in mongodb to do that we would type DB the name of the collection we're working with in this case students we can either update one document or update many let's update one we will use the update 1 method there are two parameters set up filter and update filter is the selection criteria for the update So within a set of curly braces we will pass some criteria let's update anybody's name that's SpongeBob it's basically just selection criteria the second parameter it's another document body is the update parameter what are the modifications we would like to apply we will utilize the set operator which is preceded with the dollar sign the set operator replaces the value of a field so after the set operator add a colon then another set of curly braces yes there's a lot of curly braces in mongodb So within this set of curly braces after the set operator that's where we can make our changes you can add or change the value of a field Spongebob doesn't have a full time field we can add that with the set operator if this field already exists we would update it so let's say SpongeBob is a full-time student let's execute this command it was acknowledged that's true we have a matched count of one and a modified count of one let's take a look at Spongebob DB dot students dot find name SpongeBob and here he is he is now a full-time student with your filter argument if you're working with a large data set it is possible that two people can have the same name it would be safer if we were to update by an object ID these IDs are unique for each document let's update but with the filter we will search by object ID so again that's DB dot students dot update one first is the filter parameter within a document body let's select underscore ID colon then paste that object ID for SpongeBob for every document the object ID is unique just in case there's two people named SpongeBob we won't update it looks like I have double underscore ID let me fix that real quick otherwise we'll get a syntax error so select the document that has this object ID then the second argument which is comma separated will be the changes to be made so we will set colon another set of curly braces let's set full time to be false SpongeBob already Works a full-time job he'll just be a part-time student okay that was acknowledged let's take a look at Spongebob again using the find method db.students.find ID colon the unique object ID and here's SpongeBob the full-time field is set to false before it was true but we updated it now you can unset a field too let's get rid of the full time field for SpongeBob DB dot students dot update one we will search by ID then for the second argument we will use the unset operator which will remove a field so within another set of curly braces let's take our full time field colon now to remove a field for the value just set it to be an empty string let's take a look that was acknowledged let's select SpongeBob again db.students.find all right and here's SpongeBob again that full-time field is gone now you can use the unset operator to remove a field now let's talk about update many using the update many method we can update many documents at once DB dot students.update many this method has the same parameters as update 1. if I would like to select every document I will use an empty set of curly braces what are the changes we would like to make let's use the set operator let's set everybody's full-time field to be false if they don't have this field one will be created for them all right let's find every student that was acknowledged db.students.find everybody everybody should have a full-time field that's set to false full time is false here as well yeah and that applies to everybody all right let's unset the full-time field for Gary and Sandy TV DOT students.update one let's select name Gary we will use the unset operator then add the changes to be made full time then we'll remove it by placing an empty string I'm going to copy this line we'll reuse it later that was acknowledged then let's remove full time for Sandy that was acknowledged as well so let's double check DB dot students dot find so SpongeBob and Patrick both have this full-time field Sandy and Gary don't they were unset with the update many method we can check to see if a field exists or doesn't exist within the selection criteria if any student doesn't have a full time field let's update it DB dot students.update many what is the selection criteria let's say the selection criteria is full time field does it not exist we will use the exists operator exists preceded with the dollar sign then add false does this full-time field not exist that's the selection criteria if a document doesn't have the full-time field we'll give them one so the second argument will be the changes to be made we will use the set operator colon another set of curly braces anybody that doesn't have the full-time field let's set that to be true full time true press enter that was acknowledged let's take a look at our students db.students.find all right so Sandy and Gary are both full-time students now SpongeBob Patrick and Larry are not full-time students they're part-time students that's how to update documents in the mongodb Shell you can use update 1 or update many let's do the same thing with compass hello again everybody in compass to update a field of a document you can press the pencil icon to edit the document to remove a field next to the field name you can press the trash can icon to remove it then update to add a field go to the pencil icon again after a field you can add a new field by pressing the plus button to add a field let's add a full time field for the value I will set that to be false for SpongeBob then you can change the data type here we'll change that to be a Boolean then update to change a pre-existing field again go to the pencil icon select your field then you can change one of the values but I'll keep this as false that is how to update documents in compass it's fairly easy and straightforward alright everybody so that is how to update documents in mongodb you can use the update 1 method or update many you pass in a filter for the selection criteria then the modifications within the update parameter and that is how to update documents in mongodb hello again friends it's me again today I will be explaining how we can delete documents in mongodb before we do begin this lesson I recommend exporting your collection we'll be deleting documents adding them back in is kind of a pain in the ass unless we're importing them so let's export our collection export query with filters that's fine select output you can export as a Json file or as a CSV file let's export our collection as a Json file you can also select a destination I'll place this file on my desktop just for convenience and export we'll be importing this Json file at the end of this lesson and close within Compass to delete a document all you do is press the trash can icon then delete it's really easy in compass in the Shell it's a little different in the Shell to delete a document type DB dot the name of the collection students then there's delete one or delete many we'll begin with delete one we have one parameter a filter parameter it's very similar to the find method we list some criteria to search for let's delete anybody with the name of Larry that was acknowledged the delete account is one let's find our studentsdb.students.find and Larry is no longer at the bottom we have Patrick Sandy and Gary remaining we can use delete many to delete more than one document DB dot students dot delete many pass in our criteria anybody that has a full-time field set to false will delete so we have deleted one person that matches this criteria DV dot students dot find we have deleted Patrick we can delete any documents that are missing a field or if a field does exist we can delete that let's delete any document that is missing a registration date DB dot students dot delete many we are checking if there is a registration date field does this field not exist if it doesn't then delete that document we will use the exists operator colon false if this field doesn't exist delete that document so we have deleted two documents if we take a look at our students now db.students.find we no longer have any students so that's how to delete one or many documents with the mongodb shell now if we head back to compass then refresh we have no more students let's import our collection that we exported because we do need some data for this series add data import Json or CSV file we are looking for our students Json file select and import and we're done so that is how to delete documents in mongodb hey everyone today I need to explain comparison operators operators are denoted with the dollar sign comparison operators return data based on value comparisons in previous examples we would type db.students.find then list some criteria like if we're looking for SpongeBob we would say a name SpongeBob now what if you would like to find everybody but SpongeBob well you can use the not equals comparison operator we would enclose it within a document body pre-seed our value with the not equal comparison operator dollar sign and E colon find every name that's not SpongeBob that gives us four records Patrick Sandy Gary Larry SpongeBob isn't within here that is the not equals comparison operator return every document that has a name not equal to SpongeBob let's go over a few more let's cover less than and less than equals two DB dot students dot find everybody that has an age that is less than so dollar sign LT for less than 20. that gives us one document Gary who's 18. or we could use less than equal to change LT to LTE that means R20 is inclusive uh maybe let's change this to 27. return every document where the age field is less than or equal to 27 that would give us two results Sandy who's 27 exactly and Gary who's 18. let's do greater than return every document where age is greater than 27. that gives us three documents SpongeBob who's 30. Patrick who's 38 Larry who's 32. let's do greater than equals that would be GTE find everybody that's greater than or equal to 27. that gives us four records SpongeBob Patrick Sandy who's 27 exactly and Larry so that's less than less than equals to greater than and greater than equals to you can use more than one comparison operator to find values within a certain range let's find every student whose GPA is between three and four DB that's students dot find we are searching for GPA let's use the greater than equals to comparison operator return every GPA that's greater than three comma and less than or equal to four so these would be the students that have a good GPA SpongeBob and Sandy SpongeBob has a GPA of 3.2 Sandy's is 4.0 you can use more than one comparison operator to give you a range now another good operator is the in operator with the in operator we can return all records that have one of these matching values so db.students.find let's find any student whose name is within this array we will pass in a document body use the in operator colon then list an array an array of values let's find anybody whose name is either SpongeBob Patrick or Sandy So within an array we will list our values return anybody whose name is SpongeBob Patrick or Sandy there SpongeBob Patrick Sandy with the in logical operator you will return all documents where one of the values in this array is found within this field you can also use not in which is nin so let's copy this line clear our screen paste it change in to nin not in return all documents where name doesn't have one of these values that would give us Gary and Larry we are given everybody's name that's not SpongeBob Patrick or Sandy let's switch to Compass and I'll show you how we can do the same thing within Compass you would write your filter within this text box return everybody who has a GPA field that is greater than or equal to three GTE three that gives us SpongeBob and Sandy give us everybody who is let's say age is less than 27. that gives us Gary all right everybody so in conclusion comparison operators return data based on value comparisons and those are comparison operators in mongodb hey everybody today I need to explain logical operators logical operators return data based on Expressions that evaluate to be true or false there's four and or nor and not let's begin with and using the and logical operator we can check to see if two expressions are true let's begin with DB dot students that's the name of the collection we'll use the find method pass in a document object we will use the and logical operator it's preceded with the dollar sign it's an operator then a colon whatever Expressions you would like to check you will place within an array list as many Expressions as you would like let's return any document where the student is a full-time student and their 22 years or younger usually most students when they start college they're between the ages of 18 and 22. so let's find all those students we'll have two conditions we need two document bodies our first condition is full time true give me any student that's a full-time student and our other condition is give me any student that's age is less than or equal to 22. we'll use the less than equals comparison operator colon 22 and that's it we have one student Gary he's 22 and under and a full-time student so with the andological operator you can have more than one condition this condition needs to be true and that condition needs to be true we were given the results of Gary He's the only student that passes these two filters they both evaluated to be true let's copy this method we'll reuse it let's discuss or change and to or with the or logical operator at least one of these conditions need to be true give me anybody that's a full-time student or anybody that's 22 or younger that will give me two documents Sandy she's older than 22 but she's a full-time student Gary is 22 and under and a full-time student so with the or logical operator at least one condition needs to be true so again let's copy this method we'll reuse it then we will talk about nor so with the neurological operator nor we will return any document where these conditions are false this needs to be false as well as this needs to be false it's kind of like and but instead of both conditions being true it's the opposite they both need to be false so that would give me three results SpongeBob he's above 22 he's a part-time student Patrick is 38 he's a part-time student Larry he's 32 he's a part-time student with the neurological operator we need both of these conditions to be false that gave us these three documents for SpongeBob Patrick and Larry lastly we have not suppose I have this query db.students.find any student that is not above 30. I could write something like this age colon will use the less than comparison operator colon 30. find any student under 30. normally this would give me Sandy and Gary you know this does work but what if somebody doesn't have some information for their age like let's change Gary's age to null because in SpongeBob lore Gary is a pet snail his age is in cat years just to make this easy and Compass I'm going to edit Gary's age to be null let's write that query again give me any student whose age is less than 30. well that only gives me Sandy Gary's age is null so another way in which we could return all students that are not 30 or above we can use the not logical operator the not logical operator reverses what you're looking for so let's enclose this document body within another document body then we will use the dollar sign not logical operator but we will change the comparison operator to be greater than equals give me any student that is not greater than or equal to 30. so that should give me both Sandy and Gary even though Gary's age is null in many circumstances you could use less than but a benefit of the not to logical operator is that it would give you null values return any document that doesn't fit this condition basically speaking not just does the opposite alright everybody so those are logical operators they return data based on Expressions that evaluate to be true or false and those are logical operators in mongodb hey everybody today we are moving on to indexes and index allows for the quick lookup of a field however it takes up more memory and slows insert update and remove operations use them wisely utilizing an index if you're familiar with data structures and algorithms we are storing our data as a b tree if there's a field you search for but you don't do many updates I would recommend an index here's an example let's find any students named larrydb dot students dot find find anybody named Larry I'm going to make one adjustment though let's method chain the explain method and pass in this string execution stats that will give me the execution stats of this query I have five documents within my collection we have examined five documents to find Larry we're performing a linear search through our collection we're examining each document one by one I only have five documents that wouldn't take too long imagine I have thousands or hundreds of thousands of documents well a linear search is going to take a really long time we can speed up the lookup process by applying an index to apply an index to a field you would type something like this DB dot the name of the collection dot create index now what field would you like to apply an index for let's apply an index to our name field name called in you can arrange the data in your index in ascending order by using one or descending order with negative one so with a name one would be alphabetical starting with a to z let's hit enter this is the name of our index name underscore one let's perform that query one more time DB dot students dot find anybody named Larry name Larry then we will method chain the explain method pass in the string execution stats let's see what the results are this time we only examined one document examining one document is a lot less time consuming than examining five documents like I said I have a small data set it doesn't appear to make that big of a difference but if you have hundreds of thousands of documents it's going to save you a lot of time to get all your indexes you can use the get indexes method db.students dot get indexes and we have two there already is an index applied to object ideas we have that by default and here's the index we created to look up names quickly we have two indexes technically to drop an index you'll need the name let's copy that DB dot students dot drop index place the name of the index within the drop index method and our index is dropped let's take a look at our indexes again db.student.getindexes and our index is gone now if you would like to create an index in compass you go to the indexes tab create index select a type or field name let's create an index for name select a type let's place our name in ascending order you can select more options but that's a little Advanced for us at this level let's begin with the basics and that's how to create an index in compass then to drop the index you just press this trash can icon type the index name underscore one and that's how to drop an index so in conclusion applying an index to a field allows for quick lookup of that field but it takes up more memory and it slows insert update and remove operations because you have to update the binary search tree use indexes wisely personally I would recommend an index if you're doing a lot of searching but not a lot of updating so that's an introduction to indexes in mongodb hey everybody with the last few minutes remaining of the mongodb in one hour series I thought I'd get a little more in depth with collections as we've learned at the beginning of the series a collection is a group of documents and a database is a group of collections within your database to show collections you can type show collections currently we only have one students let's create another though let's create a collection of teachers we'll use the DB dot create collection method pass in a name for this collection let's create a collection of teachers so this would work however there are some additional options that we can pass in as arguments to this method let's add the next argument it's comma separated another argument that we can pass into this method when creating a collection is a Max size for this collection we will set the kept field to be true that's informing mongodb this collection should have a maximum size this field is either true or false if capped is set to true you do need a minimum size specified so size that's a field then set a size in bytes if you would like a maximum size of 10 megabytes that would be 10 million 10 million bytes let me count my zeros yeah that's accurate and or you can have a maximum number of documents you will set the Max field to be some amount of documents I don't want any more than 100 teachers our collection has a maximum size of 10 megabytes we've also set that there can be no more than 100 documents in this collection we don't want any more than 100 teachers there are a few more advanced options for collections but that's beyond our level right now but another you may be interested in is auto index ID so that is a separate argument it's comma separated we need a document body Auto index ID this is either true or false based on indexes in the last topic automatically we apply an index to object ideas but we can toggle that to be true or false there are some pros and cons with indexes and index allows for quick lookup but any write operations are a little bit slower I'll set auto index ID to be false let's execute this command all right that was approved let's show our Collections show collections and we have two collections a collection for students and a collection for teachers hey let's create one more for practice let's create one more collection DB dot create collection what's the collection name let's create a collection of courses like what classes can we take at our school that was approved let's show our collections and we have three courses students teachers then to drop a collection you type DB the name of the collection let's drop our courses dot drop method that was dropped let's show our collections again and we are back to two collections a collection of students and a collection of teachers all right everybody that was mongodb in one hour hey if you've made it this far be sure you've smashed that like button leave a random comment down below and subscribe if you'd like to become a fellow bro
Info
Channel: Bro Code
Views: 964,219
Rating: undefined out of 5
Keywords: MongoDB course, MongoDB tutorial, MongoDB, MongoDB database, MongoDB NoSQL
Id: c2M-rlkkT5o
Channel Id: undefined
Length: 60min 0sec (3600 seconds)
Published: Fri Apr 14 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.