Tips to fast queries with feathers mongoose

Learn how to query data really fast in MongoDB with feathers mongoose.

/

Nov 20, 2020

(3 min read)

post cover

FeathersJS is an HTTP and real-time framework to create really fast and scalable apps in Node and if you want can use feathers mongoose to works with schema modeling and validation.

By default, MongoDB in a small project with a small number of documents in a collection is too fast, but not in medium or big projects where have a lot of a number of documents in a single collection.

In those cases, a single query can get a lot of documents and return data over 100ms, that's too much for DB. Normal behavior MongoDB responds in μs or <10ms.

In this post, you learn steps by steps, how to perform your current queries with extra parameters, and finally with the best feature of MongoDB also in mongoose, lean.

Summary

  1. $limit: Limit the number of documents you want to get
  2. $select: Select only the fields you need to work on
  3. .lean(): Returns documents like Javascript elements, without mongoose properties.

Limit the number of documents 🧐

Maybe you knew it, but sometimes developers forget this 🙄. Limit the number of documents you want to get, this helps MongoDB returns a certain number of documents reducing transport time. 🚝

const query = { product: 'XXXXXX', $sort : { createdAt: -1 }, $limit: 1 };
orders.find({ query });


In the previous code, adding $limit: 1 we can tell to MongoDB return only one document that matches with product id XXXXX and sort by ascending.

Never do this

const query = { product: 'XXXXXX', $sort : { createdAt: -1 } };
const products = orders.find({ query });
const document = products.data[0];


This still returns all documents that match with the query, not limiting the number of documents. 💣

Select fields you really need

The second tip is to be efficient, which means select only the fields you need to work on. 🥢

const query = { product: 'XXXXXX', $sort : { createdAt: -1 }, $select: ['_id'] };
orders.find({ query });


Now with $select your are querying efficient, instead of this:

const query = { product: 'XXXXXX', $sort : { createdAt: -1 } };
orders.find({ query });


With both methods, you still can get the id of the order, but in the last, retrieving all fields 🥵.

Use the lean method

Nope, not Lean Methodology 😂. Is .lean() which in simple, returns documents like Javascript elements, without mongoose properties.

const query = { product: 'XXXXXX', $sort : { createdAt: -1 }, $select: ['_id'] };
const allOrders = orders.find({ query }).lean();
allOrders.overwrite(...); // <-- this not will work


When use?

This is a very important question and the short answer is when you need to respond to a GET request that not need to mutate with mongoose, sends the result as come from MongoDB.

Conversely, when you need to use virtuals, validation, casting, getters, setters, and .save() will not works. An example?

Well, the best example to not use .lean() is with collections that have private data like passwords. Why? Because if you set mutations over fields through virtuals, will not work. That is too risky! A few milliseconds vs expose passwords. 😥

Extra

Create indexes correctly by every collection. This post does not cover create and manage indexes, but if you're interested, leave a comment!

Happy coding! 👩‍💻




Matias L.

@matiaslopezd

Async thinking and await creating

Matias L.

Something about coding, business and life in english and spanish powered by Imprint 😎!

3 subscribers