Discord.js & MongoDB #3 - Searching the Database

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so we're back with another mongodb discord.js tutorial i'm going to show you guys how to read data from your mongodb database using mongoose so first we need to actually save some records into our database so what we'll do is we'll create a simple command that's going to allow us to save uh notes to the database so the first thing we'll do is set up a simple command in our message event so if message.content that to case starts with create note and we're using starts with because our create note command is going to uh have arguments so to get the actual uh arguments or arguments i should say because it's literally just a string that we want to save for the user uh we're going to basically just do it very simply i'm going to get the index of the space and then we're going to use that to get the rest of the string so what i'm going to do is i'm going to go ahead and use the slice or slice index of and we're going to basically get the index of the space so that's going to be the number 11 because you can count all of the characters and you're gonna see that the space is going to fall on position number 11. so this is how the command is going to look like right we basically want to get everything after this space we don't care about anything before this space because that's just the command okay so index of gives us the first it gives us the position of the first occurrence of a substring okay so once we have that position we can go ahead and get the rest of the string by simply just using the slice function we're going to pass in the index now this is going to give us everything starting from the uh the index that we passed in so it's going to include that index as well so that means it's going to include the space and we don't want the space so we're going to add one and that'll give us the actual entire description everything after the command with the space is going to give us everything after that okay so i'm going to just console.log both the index and description real quick just so you guys can see actually let me do this separately okay there we go and let me go into my server so let's just go ahead and decree no hello world and you can see over here we have 11 which is the index of the first occurrence of the space and then we have the note okay so that's fairly simple so to actually create the note like i said we need to save it to the database but how do we save stuff again well we need to use a schema for that so we're going to go ahead and just create a quick note schema we're going to make this very simple and i'm just going to copy and paste everything so we have to write everything out and just change up some of these things and all i'm going to do is i'm just going to save the description and schema types string and i'll just save the user id of the notes and i'm not really going to play around with the options too much though you can if you want to you can set it to be required actually you know what i'll just do that real quick because it's better practice might as well right uh we'll do required and then let's just copy and paste this and there you have it so that's our note schema so now we can import that up here like so all right so now let's save the note by simply just doing awaits note.create we're going to pass in the description and we're going to pass in the user id like that okay so that's just the description since uh the value that we want to map the description to is the same we can just pass it in like this so we don't have to do description description like that all right so we're also going to wrap this inside a try catch so we want to always handle our errors so in case if an error does happen we can send a message to the user and error occurred i don't even let me actually do this failed to save notes and then we'll do saved note successfully let's see all right so let's try this out and we're going to go into our mongodb compass and we're going to look at our data so let's try it out note save and if i go inside here you're going to see we have our note over here perfect okay great so we know that this works now how do we actually read this data from the database well when it comes to searching in a database there's multiple ways that you can actually search and how you want to retrieve your data and what you want to retrieve uh that depends on the method that well the method that you want to use depends on what you want to actually get so let's say for example if you want to retrieve an individual item well you have to narrow down what you want to achieve what you want to retrieve based on a certain unique value otherwise you're going to end up with a bunch of different records that you might not even want so basically searching can be as vague as possible but it can also be as specific as possible and it's up to you on how you want to search for that i highly recommend you guys visit the mongoose js documentation and look at several examples of how they search i'm only going to cover a couple of basic examples of searching in this video so what we'll do is we'll simply just create a command that retrieves notes so first let's go ahead and get notes okay and we might have arguments too so we'll leave it alone okay so what we want to do is we want to create uh we want to basically make it so that if there are no arguments we're going to get all the notes so const uh let's do this that's message.split okay so because we want to also be able to retrieve notes by an id so for example let me actually show you guys the signature of the command so the command could look like this it could look like get notes get notes uh you know and then the id like that so we want to be able to retrieve notes like that okay so if the args the if the length of rx is let's see if the arg's length is zero then that means we are going to go ahead and retrieve the note based on right we're just going to retrieve all the notes if the arg's length is 1 then we're going to go ahead and retrieve the note by the id so what we're going to do is very simple so i'll do this if let's save args dot length so this means that there is actually um there's actually an argument okay let me just write some console law real quick argument found else let's draw this command and make sure that it works so you get notes all right so this is argument found so it seems like this is not working the way we expect to so if rx.length is equal to zero if so if there's no arguments then we will search the entire database okay so and if there is an argument then we will just retrieve it by that id okay so what we'll do is we'll just go ahead and do constant nodes equals awaits node.find okay we're going to call the find method okay and what we want to do from here is we want to pass in a condition or a filter so basically it's going to filter out what we want to actually search for because if you do dot find it's going to give you every single document it's going to give you everyone's notes and you don't want everyone's notes you only want your notes right so to do that we can pass in the condition and basically following our schema right you can search based on the description but it has to be exact or you can search based on the user id well you can also use regular expressions too if you want to search by substrings but we'll search by user id since that's likely what you'll be doing too so we just have to pass in the id and that should be pretty much it now if you come to log notes you're going to notice that this is going to basically be an array let me just go ahead and send that real quick so let's see what happens uh let's see nothing's happening what's going on if rx dot length is equal to zero i'm pretty sure maybe i messed something up actually actually you know what yeah i think i know i did wrong let me see uh i see so we don't have any arguments so oh because it considers okay okay maybe that's why my logic was wrong first okay so yeah it considers the command i forgot the command itself is an argument too so we'll just change this to if rx.length is equal to one and they'll do this if it's equal to two that means we actually have an argument okay cool so now let's try this out let's wait for bots login okay so it gives us the the note but look over here you can see that it's an array okay but what if we had multiple notes right if we had multiple notes how would we display that to the user well like i said this is where searching becomes and it's not difficult but it depends on like how you want to search right if you want to search for multiple notes then obviously that's going to require you to do a couple of more tinkering with your code but it's not too difficult i'll show you how i would handle that in a very simple situation so first let's just create another note okay so get notes this is still going to give us the first note that we saved from earlier but if we want to get all of our notes then we'll do this uh let's go over let's just get rid of this let's get rid of uh this let's create a variable called description and we're going to use a for in loop so const i in notes so i is going to be the index and notes is just the array of notes and we're basically going to build up a string uh we're going to parse the index i and add 1 to it because the index starts at 0 and we want to label our notes and we're going to get the notes by simply subscripting them so notes subscript i and then description then we're going to add a new line character and you'll see what it looks like i know it might be a little bit confusing but you'll see what it looks like in just a sec we'll send this string to the user so this will send all the notes to the user but there's going to be a problem though if you have a ton of notes in the database you're going to end up running into an error so you might want to set a limit of how many notes you want to display to the user so you can also do that by simply limiting how many notes are being displayed and i'll show you how to do that so let's just create more notes hello hi and then you can see that the notes the list of notes increases right so let's just say we want to get only five notes how would we do that well you can pass in options for the uh for the find method so the next part over here we're just going to pass in null and then for the third part we're just going to pass these are the actual options we're going to pass in so what we're going to do is we're going to go ahead and set the limits so if i look at the documentation there's actually there should be let's see should be a way to see the options okay there we go so limit okay so limits so you want to limit yourself to a certain number so we're gonna do limit let's just do three and you'll see that it's only going to give us three notes so let's try it out just make sure it works it gives us three notes okay so it only gives us the first three okay but what if we wanted to get let's say notes after a certain i don't know a certain position right you can use the skip you can use skip which will skip the first 10 over here it will skip the first 10. let's say we want to skip the first three we can do that and you can see that's gonna give us the remaining three uh wait a minute oh there we go yeah it did so yeah see it works so yeah pretty much you can use this to your advantage so i just want to show you guys how that works but you want to keep in consideration that if you have a database with a ton of data it's going to be difficult to retrieve all of them so you're going to want to use probably pagination or you're going to want to set a limit to how many notes or items the user can retrieve okay so hopefully that makes sense okay now there's also one more thing that i want to show you guys before in this video and that's going to be searching by id so quite often you want to retrieve a single item but how do you retrieve a single item most of the time you want to retrieve it based on the id of the record in our case the only thing that we can use is the object id because we don't have our own unique value that we can use you cannot use the user id or the description so we're going to use the object id so how we do this is well here's the problem if you don't show the user id to the user there's no way they'll be able to actually retrieve it so i'll leave that to a decision that you can make if you want to show the id to the user you can do that but it's up to you in our case i'm just going to kind of like i'm going to just show you how the find one or find by id works so let's just say if the rx.length is not equal to 1 and it's equal to 2. so we're just going to get the argument actually i'm going to label this as arg and we'll do at subscript of one yep and let's just console all this get notes okay so that's going to [Music] wait okay there we go so that doesn't okay there we go so that's going to hit this else case which is good and we're basically going to want to find the uh the note by the id okay so what we're going to do is we're going to simply go ahead and just use the find one or you can use find by id so find one works uh define one function pretty much just searches for one uh item in the database and you want to pass in a filter obviously so you can search for a single item based on a unique value in our situation it's better if we use find by id so we can just pass in the id just like that and that would work so let's just check to see if an item was found so if it was we will go ahead and send the description and if no item was found we would just say no not found okay so the difference between find one find by d uh let me rephrase that the difference between find and find by d and find one is that find returns an array but find one will return one item find by id should always only return one item as well because in mongodb everything is the object id is unique what i'm saying find by id i'm referring to this object id so let me go ahead and just copy and paste that so let's do get notes for you're going to see it should say actually through an error okay so the actual the the manga is actually through the errors uh that's because 4 is not a valid id but if i pass in a valid id it will actually give us the note itself let me go ahead and try this okay there we go so that's how you can search for an individual item okay so yeah it's a little bit weird using these ids but you can use them uh if you want to okay um but yeah so that's gonna be pretty much it for this tutorial uh hopefully you guys like this and actually there's something wrong with this real quick i uh i think i'll i think let me fix this actually actually um we should probably wrap this in the try catch yeah let's just do that real quick and we'll just say no not found so if we type in like a like an invalid id it's just going to uh it's just going to uh give us a message at least that's going to be pretty much it for this tutorial hopefully you guys enjoyed it and i'll see you guys in my next one peace out
Info
Channel: Anson the Developer
Views: 1,590
Rating: undefined out of 5
Keywords: how to code, javascript tutorial, how to code in javascript, async, python, web dev, css, javascript, typescript, react, react native, react props, props, class component, react component, angular, vue, front end dev, front end tutorial, react tutorial, reactjs, react router, react navigation, react class component, full stack, web development, developer, software engineer, java tutorial, java beginners, java programming, java programming tutorial, computer science, java
Id: p90RSLfwW2M
Channel Id: undefined
Length: 19min 49sec (1189 seconds)
Published: Thu Apr 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.