Introduction to MongoDB

Mongoose Query Optimizations

Scott Moss

Scott Moss

Superfilter AI
Introduction to MongoDB

Check out a free preview of the full Introduction to MongoDB course

The "Mongoose Query Optimizations" Lesson is part of the full, Introduction to MongoDB course featured in this preview video. Here's what you'd learn in this lesson:

Scott shows how to return a JSON object when a Mongoose object isn't required. Whether to start the server before connecting to the database is also discussed.

Preview
Close

Transcript from the "Mongoose Query Optimizations" Lesson

[00:00:00]
>> Scott Moss: That's basically it. There's some optimizations we can do here and I want to talk about those. So remember when I said all these crazy return like Mongo or mongoose objects. We don't really need Mongoose objects here, because we're not, the only time you ever need a Mongoose object is when you're doing an operation on the object itself like dot remove, dot populate, something you're doing on the object.

[00:00:19]
We're not doing anything to these objects and it takes Mongoose time to build that object. So we can save Mongoose the time and not build that object and just return us the pure JSON. So the way you would do that is you would do .lean on the query.

[00:00:32]
So lean means just give me the JSON, give me the object, that's it. I don't want all these fancy properties, just give me the JSON. That's very helpful, because we're sending it back as JSON. So we just wanna do that. So .lean is good for that. If you didn't want to do .lean or you couldn't.

[00:00:46]
Like for example, down here on create you can't do .lean, because it's not a query. The way you'd have to do that is you would use the .2json method on the document itself, which will convert it to a regular object. You can also do .2 object and it will also do it, but those are optimizations you can do just to get the regular JavaScript object representation of your data versus a Mongoose model.

[00:01:13]
And it does speed up database queries, especially if you're looking at a lot of stuff.
>> Scott Moss: So that's querying it, and then, from there, all you have to do is just connect to the database and then start your server. And then you've kinda got to figure out what order do you wanna do that.

[00:01:29]
Do you start your server first, and then connect to your database? Do you connect your database then start your server? There is no wrong or right answer there. It really kinda depends on what it is that you wanna do. So in this example you might see me, right now, I'm already connecting, so maybe now I'll start my server after I connect, so maybe the database is already connected.

[00:01:49]
So now, I'll start the server on my port 5,000 after the database is connected. Boom, now, I start the server. In Express that's how you start a server it's that simple. The sweet thing about Mongoose is that while the database is building up these models and collections for you.

[00:02:08]
If you start running operations on them, they'll buffer them kinda like an analytics tool does. If you ever use any analytics tool, on a Friday, Google Analytics, or literally any analytics library ever. Before it loads up, if you start issuing analytics commands and tracking commands to it, it'll capture all the events in an array.

[00:02:27]
And then when it finally loads up, it will replay them in the order it happened, so it will buffer them. Mongo does the same thing so if you start ushering commands in a database on the node model or whatever model you have, before it's done initializing. It'll just buffer those commands and replay them when it does connect, the problem with that is you might have unexpected side effects.

[00:02:47]
For instance, if you start ushering commands like find and create before the indexes were built, then you might have some code, some data that snuck in there, that snuck past the indexes. So maybe you want to wait for those indexes to be built, but indexes actually take a long time to be built sometimes.

[00:03:02]
So there's a lot of trade-offs that you have to think about when to index, when to setup, what to wait for, it's all very specific to your application. Most application you don't really have to think about that, but when it gets like really big production application, you really got to think about when do I index, do I wait for the indexes, how do I go about doing this, and it's all very case by case.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now