Transcript from the "Querying MongoDB" Lesson
>> So, we've now put one document into our collection. Let's get it out. So if I say DB dot pets, and then I say find one, just like that. There's only one document in the database. So or in the collection, so it'll get that one right So I just gave it an empty query because I knew it's going to find something and that something is going to be literally the only one there.
[00:00:28] So that's why that one ends up working. But here you can provide it with a query object. So in this particular case, I could say, type dog, and it would find the first item in the database that matched Typed dog which would also work. But if I said type cat here, it's not gonna find anything because we don't have any cats in our database yet.
[00:00:54] So find one is gonna it's gonna go into your database, it's gonna try and find the first thing that it can find that matches that particular query and then it will return that. Even if you have 100 things in there. It's just going to ignore them. If you want to find all of them Do you just do find, and this is going to give you an iterator.
[00:01:13] We'll talk about iterators here in a second. But it's going to give you an iterator to iterate all over all those. In this particular case, there's only one to iterate over so it doesn't do anything special about that. It just returns out to you. But notice here, this underscore ID thing, this is the one that MongoDB creates for you so that's it's hash of what its ideas.
[00:01:36] This is guaranteed unique as well. Yeah, question.
>> Does it always find, the same one when you use find one or is it entirely random which one it returns?
>> He asked, does it always find, the same one
>> Was it just find a random one, it will find the same one but never depend on that behavior.
[00:01:56] [LAUGH] That's a bad idea for you to just assume that it's always gonna find the same one, because if someone starts inserting, or updating, or deleting things, you can change which one is gonna find. I believe it's gonna be the last one either So it's going to be the first one created or last one modified and I don't remember which one it is.
[00:02:17] Suffice to say, though it's not it's not a behavior you want to depend on. So what I want to do right now is I want to insert a lot of items into our database so that we can start querying in interesting way, so Head back over to my course notes here.
[00:03:34] So if we come back over here and say DB dot pets count, You can see now we have 10,001 records in our database. We have 10,001 documents in our collection, the correct terminology, it's gonna take me that. As you can see the, for the most part, people use all this terminology interchangeably cuz they're all roughly the same idea.
[00:03:59] So now I wonder if I do DB dot pet stuff. Find one You can see here so I think it goes by created date so I think it'll find the first item that matches it. And it will find the first one that was created. I think is the ordering on find one But I'm not sure it's ambiguous and you never want to rely on ambiguous behavior in a that that sounds like something that would crash your app like two months into it and you wouldn't know why.
[00:04:26] So the first one here is find one we've still we saw this one already find one and we found Luna But let's do some other kind of interesting queries here. So let's say we wanted to find one of index, you know, 1337. This object here is kind of it's your query object, right?
[00:04:47] This you can use this to kind of filter in various different in interesting ways. So this returned a cat that's a havanese named Beethoven, interesting,You can put multiple things in here. So let's say we wanted to find a dog, a type. Dog, and that was of age 9, right?So it's going to match all of those, right?
[00:05:17] So this is h9n2 dog. You can see it's index eight. So that one return. That was one of the first ones that was inserted into the database. You might ask, why do we have this index thing? We don't actually technically need this. In fact, I would even tell you not to do it.
[00:05:31] But we're going to be doing some indexing later. And I wanted to have a something unique to index on so that's why that index is there. And then let's do fine. So let's say we wanted to find all dogs of age nine. So I'm just going to release that.
[00:05:43] So it's DB dot pet stuff, find type dog age nine. And you can see here we got quite a few. In fact, I think this is 20. So we got 20 different dogs here a bit of age nine. It doesn't actually return all those to you at once.
[00:06:01] So if I want to have multiple of these return, or I want to get more of them, you can see here says type it for more. It stands for iterate. So if I type it's going to give me more and more and more and more and more. So it's going to start iterating all over these results.
[00:06:17] It only gives you 20 of them at a time because it assumes Most people don't want a 1000 records all at once. If you wanted to find out how many of these actually matched, you can come in here and say count. So if I do that actually 278 pets match that query.
[00:06:34] So we have 278 dogs that are of age nine. Out of 10,000 records in our database, out of our 10,000 documents in our collection. That's this is going to be a pattern. And I'm very sorry about that, but that's good to know about it that's actually going to.
[00:06:57] That's how you iterate all these over these various different things. And you can change how many gives to you at this at one time, you can change if it gives you five records at a time or five documents or 100 documents at a time you can tell to you do all of them.
[00:07:11] In fact, I'll show you how to do that here in just a second. Let's say that you wanted to You wanted to get a typed doc and you only wanted to get like the first 40 records. I can imagine you're implementing search for like a pet adoption website.
[00:07:22] You could say give me only the first, 40 records 40 documents in the collection here, limit 40 That's count. Yeah, sorry, find. So now if I do IT again, notice it's actually done now it's not actually asking me for. So you type it for more. Now it's done.
[00:07:48] If I say IT, it says there's no cursor which basically means. You have all of them that you needed, or to demonstrate that again, if I just say five, notice it's only going to give me five objects in the database. Make sense? So that's what that limit is used for.
[00:08:12] Now let's say I wanted to get 40 but I wanted to get them all at once. I don't want to have to wait. You can say.to array like this. And now this will give me all 40 at the same time in an array, That's actually you know, formats that a little bit nicer so it's easy to read.
[00:08:31] But that's what the two array does. So if you say, give me all 278 of those dogs that we were acquiring before and you said to array, it will give you all 278 right? It won't give you a cursor back. It'll give you everything So that can be expensive, especially if you're sending down to every single user 500 Records in your 500 documents in your collection.
[00:08:56] That could end up being a pretty expensive query. And I guess that kind of bears mentioning right now, a lot of the things that I'm telling you a lot of the tips and tricks and All that kind of stuff is based on keeping your database healthy and cheap.
[00:09:12] It's because databases are almost always the most expensive part of your application, at least like at the companies I worked at, foreign above our biggest cloud costs, were interacting with our databases and so a lot of these techniques are designed to keep your costs down. Like when I was working at Reddit I think our hosting cost versus our database cost, I think our database costs was almost 10 X, our hosting costs and that's, that's not super atypical.
[00:09:45] So that's why I talk a lot about cost a lot about reliability. Those are the kinda things that when you're thinking about databases, that's what you wanna think about. What if we wanna query by age and we want to find all of the senior cats in our database, right?
[00:10:00] So let's say cats older than 12th. Well, you can say db.pet start count. Cause we're just going to figure out how many of them we have you gonna say type cat, and then you're going to say age and they're going to give age an object. Okay. And so you're gonna have.
[00:10:20] Age is going to be greater than, so dollar sign GT that means greater than, and we want it to be greater than 12. So you can actually kind of provide these little sub query objects to Mongo DB, and it knows how to interpret that. So you can see here in our database, we have 833 cats.
[00:10:38] That are of age older than, like greater than that's what the GT stands for. 12 If you want to do greater than or equal to, you just add an E on there. And lo and behold, we added a couple more so now there's no 1100 if you include the age of 12, so there's a bunch of them there's greater than, greater than or equal to less than.
[00:10:58] So LTE is less than or equal to theirs. If you really want it to be verbose about something EQ does equals write. In reality, that's the same as just putting, you know, 12 there, but sometimes that is actually required that you'll need that fact I think we'll need it later.
[00:11:18] If you wanted to find out how many cats are not aged 12 you can put not equals And you can see here, we have a lot of cats that are of type cat, that are not 12 years old. And then you can also give a raise to MongoDB.
[00:11:34] So if I wanted to have like I don't know maybe breeds, it was an array of breeds right so that could be a a mix between a pitbull and a terrier or something like that. You can use in to query inside of arrays or you can put not in to not declare if something is not an array.
[00:11:52] So there's a bunch of things you can do around arrays and objects. These are called Query operators. I find myself using these quite a bit. Specifically like the greater than or the you know less than like that. Yeah, so like here's this is actually good let's do this one if you do DB dot pets let's say we want to find everything named Fido that's not a dog so you can do DB dot pets dot find anyone to be a name.
[00:12:30] Fido and you want the type to be not equal to doc I did find but let's just to count so you can see we have 834 pets that are named Fido that are not a dog.