**Working with MongoDB: Deleting and Updating Data**
When working with MongoDB, one of the most essential operations is deleting data. In this article, we will explore how to delete data from a MongoDB database using the `deleteMany` method.
The `deleteMany` method allows you to delete multiple documents that match a specific condition. The condition is specified using a filter, which is a JSON object that defines what criteria must be met for a document to be deleted. For example, if we want to delete all documents where the `make` field is equal to "Mazda", we can use the following filter:
```
{
"$or": [
{
make: "Mazda"
}
]
}
```
This will delete all documents that match either of the conditions specified in the filter. When you run the `deleteMany` method, MongoDB will return a result object that contains information about the number of deleted documents and any errors that occurred during the deletion process.
In our example, if we have multiple cars with different makes, including Mazda, running the `deleteMany` method with the above filter would delete all cars with the make "Mazda". We can verify this by checking the MongoDB shell or using a database client like MongoDB Compass.
It's worth noting that MongoDB uses a mechanism called "soft deletion" to track deleted documents. When you delete a document, it is not actually removed from storage, but rather marked as deleted and a soft delete flag is set on the document. This allows for easy restoration of deleted data if needed.
**Updating Data in MongoDB**
In addition to deleting data, we can also update data in MongoDB using the `updateMany` method. The `updateMany` method allows you to update multiple documents that match a specific condition. Like the `deleteMany` method, the condition is specified using a filter.
The syntax for the `updateMany` method is similar to the `deleteMany` method, but instead of deleting documents, we specify an update operation. For example, if we want to update all cars where the `make` field is equal to "Mazda" and change the make to "Ford", we can use the following filter:
```
{
"$or": [
{
make: "Mazda"
}
]
}
```
Inside the filter, we specify an update operation using the `$set` operator. The `$set` operator allows us to set new values for specific fields in a document. In our example, we want to change the `make` field to "Ford", so we use the following syntax:
```
{
make: "$ford"
}
```
This will update all documents that match the filter and set the value of the `make` field to "Ford".
**Running the Code**
To test our understanding, let's run some code in MongoDB. We create a simple database with a few cars, and then use the `deleteMany` method to delete all cars where the `make` field is equal to "Mazda". We also use the `updateMany` method to update all cars where the `make` field is equal to "Mazda" and change the make to "Ford".
We verify our results by checking the MongoDB shell or using a database client like MongoDB Compass. To our surprise, the code seems to have worked as expected! The car with the Mazda CX-5 has been deleted, and the car with the Mazda CX-3 has had its make changed to Ford.
**Conclusion**
In this article, we explored how to delete data from a MongoDB database using the `deleteMany` method. We also examined how to update data in MongoDB using the `updateMany` method. By understanding these methods, you can effectively manage your data in MongoDB and build robust applications that interact with your data.
"WEBVTTKind: captionsLanguage: enhello everybody and welcome to a brand new tutorial series on a simple database app with python using mongodb so for those of you that are not familiar with mongodb this is what's known as a nosql database it's super easy to use especially in python and what we're going to be doing here is actually setting up a remote database so we're going to first start by working a little bit on a remote linux server don't worry if that sounds intimidating to you i'll be walking through every step all you have to do is kind of just follow along with me to get things set up and what that means is that the database that we create is not just going to be local on your machine it will actually be hosted in the cloud so say you actually want to make some kind of real application that maybe you're going to deploy or that people from different machines you're going to use no problem this will definitely work for it and that's why i wanted to do this series to show you how we can actually set up a remote database so anyways once we get that database set up then i'll be talking about how we actually work with mongodb how we can connect to that database and then we'll wrap kind of our aim of this series which is going to be to just learn about databases to a simple project and the project that i'm going to be talking about here is kind of a basic car dealership system so what i'm going to do is have it so that we'll have a bunch of cars in the database a user can kind of search through and look for all of those cars and then they can quote unquote purchase a car and i'll show you how we would handle the database operations around that and then the user input and all of that with that being said sorry for the long intro let's get started in just one second after a quick word from the sponsor of this series who's actually going to be allowing us to use all of this database technology for free i need to start by giving a massive thank you to lenode for sponsoring this video and this series i've been using lenod for well over a year now and i can personally attest to what a great company they are in fact they are so great that they've actually set up a live event on september 29th at 11 a.m est for me to run through the second part of this series live with a ton of time for questions and interactions from all of you you can sign up if you want to attend this live event from the link in the description if you can't make it on the 29th register anyways and you'll get the on-demand lab when in your inbox now let me tell you a bit about lenode so lenode makes it easy and affordable to host your own app service or site in the cloud maybe you started with an entry-level hosting service when you were new to coding lenode is your step up to powerful fast fully configurable cloud computing with server plans starting at just five dollars plus no hidden fees or surprise overages lenode offers no-nonsense hosting at a price you can afford lenode has a full api integrating with tools like terraform and kubernetes making it the perfect environment for development projects get started today by heading to the link in the description and you'll be given access to a hundred dollars in free lenode credits so one last thank you to lenode now let's go ahead and continue the video alright so once you've signed up for your free 100 credit and made your new account on lenode we should be good to go and actually create our first server so obviously the first step here is going to be to make a server the server is going to store our mongodb instance if you want to call it that and we will connect to the server when we actually want to use mongodb now it's really easy to do this to actually set up the server we're going to press on create we're going to click on lenode which is just the standard server that we want to use and then we're going to actually go ahead and click on marketplace now just note that within here you can select like all your different plan options what you want like root password you can make the server as configured as you want to whatever your need is but in our case we're going to go to marketplace and select the mongodb image right here now essentially what this does if we select one of these here and you can see there's a bunch of them like github minecraft so on uh this just sets up the server to automatically have this technology or tool or whatever it is installed on there for us so we'll select mongodb that means we don't need to install that and set it up that's going to be really nice and now what we're going to do is enter our mongodb password now make sure you remember what this password is that's going to be mine i'm not going to tell you guys what it is because we are going to need to use that when we actually set up mongodb next regions i live close to toronto so i'm going to select toronto obviously select the server that's closest to you or where most of your users would be next we get to choose what plan we actually want for our server note that the cheapest plan here is the nan node it's five dollars a month one gig of ram one cpu that'll actually be enough if you want to use that for this tutorial now since i have a few credits on here i'm just gonna use lenode four gigabytes so four gigs of ram two cpus and again that will be more than enough that we need for this tutorial keep in mind that if you're going to have an app that has a lot of database read and write operations you're probably going to want to go up to maybe 8 gigs of ram or mess around and see what you actually might want to use here for your hardware but obviously the more the better the hardware the faster your database is going to be next you can change the name if you want i'll just leave it like this and then you need a root password for the root password again i'm not going to tell you guys what this is because you will see my server ip so you'll be able to log in but select something that is a strong root password you don't want to get hacked on your server obviously and then press create now this is going to take a minute or two to actually create the node it just has to set everything up install mongodb and all that stuff once that's done i'll be back and i'll show you how we can actually get into our lynn node and start working on it directly so myla node is ready to go now what we need to pay attention from this page before we leave it is just the ip address so grab the ip address you can copy it just by pressing the button we're going to need that for our server and then obviously remember your root password and your mongodb password those could be the same we're going to need both of those in a few steps in just a second all right so now what we need to do is actually get onto our server and start working on it so what we're going to do is download putty this is a tool that we can use to just ssh into our server and actually start working on it uh well remotely i guess if you want to say so from this computer we can access it we can run commands on it and do what we need to do so i'm going to download putty actually i already have it downloaded but i will leave a link in the description where you guys can get it from once that's downloaded open it up and then we will go through the next steps all right so once putty is open what i'm going to do is simply input the ip address right here in the bar where it says host name or ip address and then i'm going to make a new profile and save this in here so i'm just going to say tutorial you can see i've been testing this out before and i will simply press save that will save the ip address and any of the other settings i choose here now just to make it easier for you guys to read you don't have to do this i'm going to go to appearance and change my cursor size or font size sorry to be 22 not 222 22 so you guys can actually read this terminal window when it comes up all right so then once you've done that you can go back to session you can press on your profile and you can press save if you change the font size and that will just save all the settings next i'm going to press open you might get a pop-up like this if you see this press yes i'm not going to explain really what that is but you can just press yes and then we're good to go now we need to log into our server so assuming your server is up and running and you type the ip address in you should be following along at this point and at this step we're going to sign in as the root user and then we're going to use the roots password which is the one we inputted when we set this server up so i'll type my password in you should see if the password is correct you get something that looks like this and now what we're going to do is start setting up our mongodb server so there's a few steps here they're not super complicated but you need to make sure you follow them precisely if you don't you're going to have problems connecting to the mongodb server and it's going to be a lot more difficult to go back and figure out what you missed rather than just doing it correct the first time so the first thing we're going to do is we're going to type hyphen u and then we're going to type admin hyphen p hyphen hyphen authentication database admin now what this is doing is pretty much starting the mongodb shell and we're saying okay we're going to use the admin user which is the default user for mongodb it always has an admin user and we're going to use the authentic authentication database which is admin what we're going to do here as soon as we get inside of the shell you can see it's asking me for a password for admin this is going to be the password we inputted when we were setting up the server what we're going to do here inside of the shell is we're going to add a new user that is going to have access to a certain database the database that we'll be using for our project so the first thing we need to type is use admin what this is saying is use the admin database so what we want to do is add a new user into the admin database that's not actually the admin user because we want to give our application its own user to access the mongodb database and we just need to add its credentials to this database so what we need to do now is we need to type db.create and i can't zoom i can't scroll up unfortunately but we'll say db.create user and then what we're going to do is inside of curly braces inside of other parentheses like this we're going to say user colon we're going to put the name of the user so in this case i'm just going to pick tim we're going to go comma pwd which stands for password obviously we're going to put our password in double quotation marks i'm just going to make my password tim that's fine you guys will be able to log into the server but i will change it after and then next we're going to say roles now this one is important what we put inside of roles is what we want our user to be able to do so if we want them to be able to read from a certain database or write to a certain database we write it inside of here so in this case we want the user that we're setting up here which is tim to be able to read and write from whatever database we're going to use when we actually start connecting to mongodb so we haven't created this database yet but we're just going to put in what the name of it will be and then our user will be authenticated on that database when we eventually create it so what i'm going to put inside of here is curly braces so i know this seems a little bit confusing we'll add some text on the screen so you can see exactly what this looks like but i'm going to put curly braces i'm going to put rule and then colon and then inside of double quotation marks i'm going to type read write like this it needs to be spelled like this if you just want it to be read obviously you would just write read if you just want it to be right you would just write write and this is telling us what permissions this user is going to have for whatever database we specify here now i'm going to type db this is saying okay what is the database name that this user has this role for and in this case we're going to call this dealership so the database we're going to call use is dealership i know it's cutting off again we'll put the text on the screen and now i can simply press enter and we can see that successfully added the user so if you get that text you are good to go what we can type next is quit uh oops quit sorry with two brackets that will exit out of the mongodb shell now what we need to do is simply set up authentication and make sure our server is listening on the right ip address and then what we can do is connect to it and actually start writing some python code so the next thing that we're going to do is we're going to type sudo nano nano is simply kind of the text editor for linux and then we're going to type slash etc mongod dot conf so this is simply the configuration file for our mongodb database what we're going to do here is once we press enter we're going to actually just make sure that this is listening on the right i p address and then enable authorization sorry to make sure that we can't access this database unless we're an authorized user by default uh we off authorization is not enabled so we have to enable it so first thing we're going to do is we're just going to use the arrow keys on our keyboard to go down and move to where the net section is where it says network interfaces and what we're going to do is change where it says bind ip to say instead of 127.0.0.1 0 000 like that so 0.0.0.0 what this is saying is bind rip to our public ip address rather than our local ip address so if we just have it if we left it the way it was what that would mean is we can only access this server if we are on this server so it's like running on the local host if you want to think of it that way obviously we don't want that so we want this to run on the public ip address so we'll change the bind ip to be that next we're going to go to the security page or the security tab or whatever you want to call it notice how there's a little i guess what do you call this number sign here at pound sign we're just going to delete that and then under security we're going to type authorization so off oops let's not tab all the way in we're going to type two spaces sorry and then off oriezation make sure you spell it correctly and we're simply going to say enabled like that and actually i have to put enabled in quotation marks my apologies just checked that but authorization and then colon enabled so what this is going to do is make sure that we cannot access the database again unless we are authenticated and we will use the user we just created to authenticate all right so to save this you're going to type control x on your keyboard like that and then you're going to simply press y next you can press enter to say yes this is the file name i want to save it to and then it should save last thing we need to do is restart mongodb and then we can get out of this server and start writing some code so to restart mongodb we're going to type sudo service d and then restart like that so give this a second it will restart the mongodb service now we can actually close putty and get out of it and we can go ahead and connect to this from some python code all right so we've almost reached the coding part there's one more thing we need to do which is install what's known as like an engine or a driver for mongodb usually mongodb works with javascript but since we're going to be using it with python what we need to do is install module called pi so what what we'll have to do is go into our command prompt or terminal if we're on windows or linux or mac obviously the appropriate one for that we're going to type pip install pi now of course you need python installed on your system for this to work i'm going to assume you have that already if you are on windows this is the command you want to type if you're on mac you want to type pip 3 install pi and if you're on linux it will be pip3 as well so go ahead and type that press enter you'll see for me it's already installed but if for some reason this command does not work for you that means you don't have pip installed or it's not on your system path so what i will do is i will leave a link in the description as well as a card in the top i believe it'll be left hand corner i think i'm pointing to the right one and you will see a link that says how to install pygame don't worry you can follow along with that video and it will show you how to fix your pip and instead of typing pygame you're just going to type pi so pi is obviously the module we want to be installed anyways follow along with that once that is installed in your python interpreter or your python version we're going to open up a new python file i'm going to use vs code for this tutorial but you can use whatever you want and we're simply going to type import pie game like that or sorry what am i saying pie game import find i've been using pi game too much recently now what we're going to do is just press the run button here and just make sure that everything runs we don't get any errors and if you've made it this far then we are good to go and we can now connect to our server so remember i said keep track of that server ip address don't worry if you forgot it you can go back to the node website and grab it in fact i might have to do that in one second but what we're going to do is import pie and then we're going to say client equals pie dot client like that and inside of here we're going to type kind of a url that will allow us to connect to our server so let me grab my server ip address which i need and then i'll show you how we put the url in and how we connect all right so i got my ip address now what i'm going to do is i just put in a variable here just to store it for right now i'm going to show you how we type in this url so to connect what we're going to do is inside of this right here instead of client we're going to type db colon slash slash we're going to type our username for the user we just created which in this case is tim for me we're going to type colon the password for that user my password was tim i just kept it the same as the username we're going to type the at sign and then we're going to put this ip address in afterwards and then we can simply do a slash and we should be good to go this is all we need to do to actually connect to our remote mongodb server so we just go mongodb colon slash tim which is our username colon tim which is the password for that user and then at and the ip address of our server that you can get from lenode followed by one more slash and that should be all we need so what we can do now is simply press run and make sure that this works if you don't get any errors we should be working everything should be functioning fine and now what we're going to do is actually create a database create a collection and create some documents so this brings me into the first thing i need to explain about mongodb rather than having tables and schemas and all the stuff you may have in other database systems there's three main things that we talk about here in mongodb the first is a database a database can store multiple tables if you want to think of it as an sql database but really what a table is referred to in mongodb which is just a collection of a certain type of data is a collection so it actually makes sense to be called a collection but you can think of the hierarchy like database so a database like that collection and then underneath collection we have what's known as documents so one database can store multiple collections and the documents are actually the pieces of information that are in each collection so maybe we have a collection for our purpose called cars right then inside of cars we would have all of the information relating to a specific car and that'd be stored inside of the documents right so each document would store one car the collection stores all of the cars and the database stores everything related to the dealership so we'll have some other collections in here maybe like our customers maybe like any purchase orders or anything like that right that is kind of the way that this works so the first thing we need to do is we need to create a database so to create a database is actually really easy i'm going to say db equals and then client open two brackets like this and inside of here i'm going to type the name of my database so what do i want i want to name my database dealership you need to name it this because again we added the permissions for our user to read and write to the database dealership and then let's run this and see what happens all right so nothing went wrong we should have technically created this database when we just wrote that line i know this is like how did you just create a database doing that but the way it works is that if we say client which is just the mongodb client and then we put brackets like this what it's saying is okay does a database exist called dealership if it does access that database if it doesn't create a new one so we're saying db equals client dealership next what i want to do is create a collection so remember we can have multiple collections inside of each of our databases so i'm going to have three collections and what i'm going to do for this project is i'm going to have cars i'm going to have customers and i'm going to have purchases so essentially a purchase will link a car to a customer and then obviously a car and a customer pretty straightforward so i'm going to say cars equals sorry db and then inside of here i'm simply going to call the collection cars so now we have a reference to the database and this works the same way we want to create a collection we just add our braces like this it says okay does this collection exist if it does access it otherwise create one so cars equals db cars we're going to say customers equals db customers and then finally we're going to say purchases equals db purchases now technically these collections will not be created until we insert a document into them so i just want to show you how we insert something in really quickly i know i'm kind of skipping through a few things because we need to actually create the collection and then i can prove to you that it was created because we inserted something so a document is simply some kind of dictionary so if we're thinking about python if we have a dictionary like this a document is really just key value pairs so it's key and then value like that the key can be whatever we want the keys do not need to be the same in all of the documents that are in the same collection but we just have some key and then some value associated with that key so if we were talking about cars we might have something like the make right and then maybe that's equal to say ford right or honda or mazda or whatever it may be and then we would have a model and maybe that's equal to you know the for edge or whatever it's going to be that's kind of how we create a document so let me show you first of all how we insert something and we'll just insert something into the car's collection so i'm going to say cars dot insert underscore many or sorry not instruct many insert one that's leading to what we'll do next but cars.insert1 and then inside of here what i'm going to do is i'm going to put a document so to create a document i just open up a dictionary and now what i need to do is add the key value pairs so we'll delete this one after but i'm just going to say make and then i'll say this is a ford and then i'll say model and this is an edge all right so we'll insert one you're not going to see anything happen but let's just insert one and see if we get any problems we don't that's awesome all right so now that we inserted something into the cars collection let's see how we can actually retrieve it and look at it so to look at something in mongodb what we can do is instead of cars.insert we can say cars.find so i'll get into this in more detail in a second but if i want to look at all of the entries in a certain collection or all of the documents in a certain collection i simply just put an open document like this so just open braces because what we could do inside of here is add what we call a filter or yeah i guess a filter is the best way to say it or a query if you want to put it that way and that means we'll only look for documents that match what we put inside of here so technically if we pass an empty document everything will match an empty document because everything is a document right if if that makes any sense but if i were to put in here say like make colon and then forward this would only return to me cars that have the make that is equal to ford so again we'll get into that later but i just want to show you how we can look at something so that we can prove that it's actually in here so result equals cars.find and then i'm going to print result like that so let's run this and you can see that if we have a look here we get piemongo.cursor.cursor object at some memory location so obviously that's gibberish to us that doesn't mean very much so what we need to do is actually figure out okay well this returned to us something but how can i see what that is so since this actually returns to us a cursor which we just saw what we need to do is actually iterate over that cursor if we want to get all of the values that are from it right so i clearly can't just print result what i need to do is actually loop over it and then what i can do is look at all of those results and do something with those in my python code so what i can do is say for results in and this should really say results because there could technically be multiple things here if we had inserted multiple documents and now what i can do is print the result so let's run this now and notice i know it's popping up small here we have this document that tells us an id a make and a model and notice this is the make and model that we inserted so that is kind of the basics on how you grab and how you insert something obviously i'm going to go into more in depth and first though what i want to cover is this thing called id so notice that there's this underscore id it's equal to an object id what that is telling us is this is the unique identifier for this document so every time we insert something into a collection it needs to have a unique id now we can set the id ourself if we want or what we can do is simply just let mongodb do it for us so if when i inserted something i actually added a field that said underscore id then that id that you just saw here would be overwritten with whatever we passed it but since we didn't give it the actual id it just automatically generated one for us so that's actually a benefit of using mongodb and inserting it like that it can just generate the id for us but obviously if we want we can override the id and have it be like id1 id2 as long as it's unique and there's not going to be something else that's inserted with the same id no problem we're good to go and of course the reason we need a unique id is because we need a way to differentiate documents even if they have the same information in them so what i'm going to show you now is actually a tool we can use to view our databases and collections and documents so we don't have to keep running these manual queries when we insert something in the database so what you can do is go to the link in the description it's called mongodb compass and this is just a really cool tool it's made by mongodb themself and allows us again to connect to the database and then just view it so select whatever version you want i'm going to go with just the windows 64 bit 7 plus the exe version and press download now mine's actually already downloaded so i'll cancel that but once you guys finish the download then obviously come back and we're going to open up that tool and i'll show you how it works all right so once you open up the tool all we actually need to do is just grab the url that we use to connect here from our code except we don't want the quotation marks this time obviously and we're just going to pass it in here so just do control v and you can see that we'll just have mongodb colon slash and then username password so username colon password at and then the ip address that it's at we'll press connect that should authenticate us and you can see all of the databases that we have notice you can actually create stuff from here as well so you can mess around and just actually create kind of some data if you wanted to query it on your own or something like that but anyways i'm going to press on dealership notice we have a collection called cars note that even though i made customers and purchases there's no collection there yet simply because we haven't added a document into the collection so it was not created all right so now i'm going to press on cars and when we go inside here we can see that car object that we made we have make forward and model edge so we'll use this tool i'm going to leave it open in the background just so that we can check what's happening with our database when we start performing some of these operations all right so i've showed you how to add something in i've talked about what a document is i want to be very clear that when we create documents and we put them inside of a collection they don't need to be the same so if you were looking at like a standard sql database every single document that you would put in there every single entry or row had to look the same meaning it had to have the same amount of columns it had to have a certain type of information it's what's known as a schema that was being enforced on the data you were sending in now with mongodb that is not the case we can store anything we want in the collections so that's good and that's bad but just keep that in mind so what i want to do right now is i actually want to add three functions inside of here that let us add information to the cars database the customers database and the purchases database so i'm going to go through this quickly because it's pretty straightforward but first we're going to say define add cars and what i'm going to do is pretty much define what i want the cars database or cars collection to look like so what keys do i want in there what values do i want in there well what i want is i want a make i want a model i want the year of the car i want the engine horsepower we'll just put a lot of stats here so it's interesting later on when we do some queries i'm going to go with the vehicle actually no i don't want to do that one let's do make model year engine horsepower msrp meaning the price of it and then we'll add a actually that's all we need for right now okay so these are the few things that we're going to have to pass to add cars or add car when we want to create a new vehicle in our database now what we'll do in here is we'll say document equals and we'll actually just make the document and then we'll insert it on the next line so what i'm going to do is i'm going to say make colon make i'm going to say model inside of quotation marks with the capital so it's nice and formatted model colon model we're going to say year colon year and oops your colon year i'm going to say engine horsepower colon and then engine horsepower and you get the point we're going to say msrp colon msrp now actually this probably shouldn't be capitals we're going to make this lowercase just to make this proper msrp and finally what i'm going to do is i'm going to say date added like that colon and i'm going to import at the top of my program date time so this is built into python you don't need to install it but import date time and then here we're going to say datetime.datetime.now this is simply going to give us the current date so we can tell when we actually added this entry to the database next what we're going to say is well what collection do we want to insert this into cars so we're going to say cars dot insert and underscore one and we'll simply put the document that we just uh created in there and in fact we're actually going to return this and the reason for that is that this will return to us some kind of object that tells us about the status of what we just inserted the most important thing is it actually allows us to type the following so say i said car equals add car and i added all my entries i could type car dot inserted underscore id and this would tell me the id of this document that i just inserted the reason that's important is because we didn't set the id so we don't know it so if we want the id we have to return to this value which will give us the id of whatever we just inserted if we call dot insert id on it hope that makes sense but that's the basis of this function next what we're going to do is we're going to create the customer function so define add customer i'm going to say first underscore name last name and dob which stands for date of birth for our customer database or a customer collection i'm just going to have it so we have their first name last name and date of birth we obviously can add more but since this is a tutorial i'm not going to go crazy with all of the fields so i'll say document equals and then i'm going to say first name colon first underscore name i'm gonna say last name colon last name i'm gonna say date of birth is d-o-b and then finally i'm going to say date added and i'm going to add the same thing that we had up here so datetime.datetime.now all right so i hopefully get the hang of this next we're going to add a purchase and i'm going to talk now about what we're going to do with this purchase um what do you want to call it document collection and so on all right so now we need to talk about what a purchase actually is and how we're going to kind of set up this collection so essentially what i want to do with the purchase collection is i want to be able to link a customer to all of the cars that it's purchased now remember that each document that we add has a unique id and that's unique within the collection so technically if i have an id for a car and i have an id for a customer if i can somehow store those together i can determine that a certain car was bought by a certain customer that a certain car was like or that a certain customer bought a certain car so the other way around obviously now some people what they might do if you're familiar with other databases is they might do something like okay let's just add a field to customers that says purchases and inside of here what we'll do is we'll just store a list of all of the cars that the customer purchased now that would work technically if we wanted to figure out what cars customer purchased and store that on the customer the reason that might not be the best thing to do is because if we have any other information relating to the purchase of the car say like how much they have on financing how much money they put down what method of purchase they did when they bought the car it's going to be tough to store all of that within the customer and it doesn't really make sense to have the customer collection itself storing anything other than information about the customer so what we'll do instead is we'll simply create a purchases collection and inside of the purchases collection we will have a document that has the id of the car the id of the customer and then any other information we want associated with the purchase so let me show you what i mean before the parameters here i'm going to say car id customer underscore id and then method now method is just going to stand for method of payment i'm just kind of making up some fields so that we have some more information inside of here obviously this would usually be much larger but hopefully you're getting the point so what i'm going to do is say car id colon car underscore id and i'll explain this more thoroughly in a second customer id colon customer id and then method like this is equal to method all right and then finally we'll say date so like the date that this happened and this was purchased and we can simply just add i don't know why i needed to scroll up for that datetime.datetime.now great so then what we'll do is we'll do the same thing we'll insert this in and in fact i forgot to insert it inside a customer so we'll do that in one second we're going to say purchases dot insert one document and then return that and just finish this off up here return customers dot insert one document great so the way this is gonna work is we're gonna take an id of a document that's a customer right and a document that's a car we're going to store that inside of the purchase and then we're going to store the method and the date this way if we ever want to check what cars a customer has purchased we can look through all of the purchases and just check if the customer id of that purchase is equal to the customer that we're looking at and in the next video what i will do is i will discuss how we can do that and go through some more advanced queries and stuff like that but that is the basis for adding all of these things in and hopefully that's kind of making sense so what i'll do now is i'm going to add a customer and i'll add a purchase and i'll kind of show you what i mean all right so now let's add a customer and let's add a purchase and let's make sure everything is working in fact what i'm actually going to do is add another car you'll see why it's just going to make some things simpler for us i'm going to say car equals add underscore car and then what's the information we need we want the make model year engine horsepower and msrp so let's say the make is going to be a mazda let's say the model is a cx-5 uh what else do we need we need year engine horsepower msrp let's go 2020 let's go horsepower 250 and let's go with msrp of 45 000. i have no idea if that's correct but let's just do that next let's add a customer and a purchase let's go customer underscore yeah customer equals add underscore customer let's give it a name tim tech for the last name and then for date of birth let's go july 20th 2000 that is actually my birthday and then we'll go with purchase equals add underscore purchase and now what we need to do is we need to add the id of the car the id of the customer and the method now fortunately for us what we can do is we can simply say car dot inserted id because we just inserted it so we can grab the id we can say customer dot inserted id and we can say method the method of payment i'll just make it up and say cash all right so now these technically should be added into our database i'm going to hop over to the other tool after i run this so let's run it now and make sure that that worked all right so i'm over here i just pressed the refresh button to refresh the database here and you can see that now we have those other collections so customers and purchases if i go in customers we can see the customer i just added if i go in purchases we can see the car id customer id method and the date that was added if i go to cars we can see the two cars that we have so that is again how we actually insert all of this stuff now i quickly show you how we can actually delete stuff and then how we can update stuff all right so to learn about updating and deleting i need to quickly cover a basic kind of querying thing which i've already shown a little bit but essentially the way that we get specific data from this database is we'll use the collection so we'll say cars we'll say dot find and you could say dot find underscore one that's fine that will just give you the first one that pops up in the query that we find you can use some other ones find one and delete find one and replace there's all kinds of methods we can use so if i show you one that you haven't seen before or you think of another way it doesn't necessarily mean that's wrong it's just another way to do it but typically what i do is i say cars.find and then inside of here what i will do is have a collection and i will simply put the filters that i want to find so if i only want to look for cars that are the make of mazda then i would do this i would say make masta now if i wanted all the cars that were the make of mazda and the model of cx3 then i would say model comma cx3 now what this next parameter is there's two parameters to find here what it allows you to do is not only to filter what you want to look for but to filter the columns that you want to be returned so what you can do is you can add another set of brackets here and then you can simply say okay i want the make column so if you want the make column you would say make colon one which stands for yes i want this column you would say okay i also want the model column so i'd say model colon one and then any other columns you don't mention other than the id column will automatically not be given back to you which means it will return to you only the make and the model of whatever cars you find as well as the id now this is kind of tricky because the way they set this up is strange you can't technically like write make 0 model 1 what that would mean is just give me the model column because model has the one beside it a 0 means don't give me that you can only use ones or zeros so i say i wanted all the columns except make and model then i would go make 0 model 0 it would give me everything else other than make and model now the only exception to this is that if you add the underscore id field which is on every single um what do you call a document you can change this to be zero or one and that doesn't mean that the make and model have to change right so i could do something like id 0 and then 1 1 which means just give me the make and model don't include the id now if i said id1 and i said make 0 model 0 that means i'm just going to get the id and all the other columns except the make and model hopefully that makes sense you can mess around with it but anyways that's kind of how you find stuff so i'm going to say cars.find that's how we query and if i run over that like we've seen it'll give me all the documents that are like that now if i want to remove something it's the same thing so i say cars.remove and what this will do is remove any documents that fit this description so let's actually just do it with not the mazda but let's do it with the ford so let's say ford and let's say edge and let me just run this and cars.remove is deprecated oh sorry it's not removed my bad guys it is delete so i can say delete one and what this will do is delete only one entry that has this so if it finds this whenever it finds this whether it's going like from top to bottom or however it's searching it will just delete the first one it finds and if i go delete many this will delete anything that is um that fits this description so hopefully that makes sense this also returns an object so i can say result equals cars.delete many and then i could say result dot deleted count and that will tell me how many things have been deleted i think there's also a deleted ids maybe not maybe it's just deleted count that's telling you how many things were removed anyways let's just run this with delete many and see if this works seems to have worked i'm going to go back to the tool here refresh my cars and notice that the forward is gone for some reason there's three masses i think i might have accidentally ran that code to create the masses multiple times but you notice that that forward edge is now gone all right oh yeah that's because these are all here and i didn't delete it and i was running this a few times but anyways you get the point that's how you delete all right now i'm going to show you how we can update so instead of doing delete many you can say update many you can pick what you want to update so in this case i want to update anything that matches this criteria and then this one's kind of weird what you have to use is this like little set thing so you do a dollar sign and then set and what this is saying is okay i want to set this attribute to be this value now there's a lot more of these as well that you can use but i'm not going to talk about them here so i'm going to say set and then inside of another set of curly braces i'm going to go all right what do i want to update so in this case maybe i want to change the and i'll have to change this to be a mazda maybe i want to change the make of all the mazda cx-3s to be a forward i know this obviously doesn't make any sense but let's just do that so let's say make and then colon and we're just going to put this as ford so that is saying set this column to be this so let's actually try that and just see if this works all right so it doesn't look like anything went wrong let's refresh this and let's see and since i have cx5 here and i put cx3 there obviously that's going to be wrong so let's change this to cx5 and let's run this now and let's have a look and refresh and we can see that now the make of all of these has been changed to ford so of course you can set more than one thing inside of here i can change the model as well so i could say model colon and then change this to just be an edge so we're changing all of that to be a ford edge in fact let's just run that and see if that updates and well it's because now the make is no longer mazda so i have to change this story to be afford that's why that wasn't working now if we actually query the right thing and update it and refresh we can see that the model has been changed to an edge so that is the basic operations in mongodb that's how you update that's how you delete that's how you add and in the next video we're going to actually add more to this project to add some kind of like user interface and allow people to actually won't really be a user interface it'll be more of like a text console that will allow people to kind of query through cars we'll add a bunch more cars and we'll do um some other more interesting stuff with the database like some more advanced queries some more advanced updates deletes and just everything you would probably need to do in a project i'll be showing that in the next video so i apologize that this was long i really did want to go over everything and make sure you guys understand as well as really focus on setting up this server so you guys could authenticate and connect and be able to use this in a real production application so if you guys enjoyed make sure you leave a like and subscribe look out for the next video reminder that there is a live event running on september 29th that is kind of what the next video is going to be i'm going to be recording that live event that's hosted by lenode you guys can check all the details related to that in the description and then i will be taking that recording and i will be posting that on youtube so you guys can kind of feel like you were at the live event even if you are not able to make it so anyways if you enjoyed of course like subscribe i will see you in the next one\n"