How to auto populate fields with MongoDB and Mongoose

How to auto populate fields with MongoDB and Mongoose

In case you have used MongoDB and mongoose together, maybe you did find yourself in a situation similar to this:

The “account” model definition

In this case, we have a Schema with a property called “platforms” which is basically an array of objects.

The key point to be noticed here is that every “platform” is a model already defined in our project and what we are doing here is trying to maintain a relationship between an account and the platforms it’s valid for. If you are coming from the SQL world, this is basically a couple of tables prepared for a join.

The “platform” model definition

Depending on the use case, we would want this collections to be populated, or not, with the actual “platforms”. We have some strategies to achieve this goal.

First of all, preserve an array with references

In my experience, the best way to maintain a relation of this type is to preserve only an array of ids instead of an array objects. There is nothing bad with having an array of objects in a property of your model, actually, NoSQL is perfect for that; the problem is that we use to abuse of the liberty.

Sometimes in the past, I’ve faced the situation of having too many nested properties in a model. I ended working with very complex queries and giant documents that were really difficult to manage.

So in order to make your future yourself happy, don’t abuse the use of nested properties and go for a model that keeps references instead of the entire object.

If you have followed my previous tip, for some queries, you’ll need to populate the array of references to get the real value of them. There are some options available.


Populate manually

This is the ugliest one, but maybe the more reasonable if you are learning to work with a library like mongoose. There is no too much to explain, basically you’ll need to make a couple of queries to get the records that the ids make reference to.

A sad example of manual population

This is “ok” if you are doing this just one time in all your application and if you really don’t have time to do a minimum set up or don’t want to change the model.

But, to be honest, we always do this because we don’t know about other options. So let’s go for the more popular ones.


Populate with mongoose

Mongoose itself provides a method called populate which is the alternative for the $lookup aggregation operator in MongoDB. As we can find in the documentation:

Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s).

And this is exactly what we want to do 😉 It’s really easy to populate references using mongoose, we just have to set up some fields in the model and call the function “populate” in the queries specifying the field to be populated and voilà.

If you want an example of mongoose populate function, please take a look at the official documentation.


Use a npm package

Mongoose native populate function is great, but sometimes we want every field to be auto-populated… and it’s not cool to call the populate method a lot of times with different arguments. An awesome option for it is a npm package called mongoose-autopopulate. The package uses the same settings in the model that we did set up before and that’s it! The plugin will do the population automatically.

Example from the mongoose-autopulate documentation

I hope that these options for auto-population in mongoose are useful for you. If that’s the case please share and don’t forget to comment below if you want to read about other similar topics.

..

Regos Dev Studio is a product development company that builds add-ons for Jira, Confluence and LiveChat, combined with development of custom solutions in a variety of languages.

Don’t forget to check out our website, visit our Atlassian marketplace listing  and our apps for LiveChat. You can also follow us on Twitter and stay tuned for updates!

Leave a Reply

Your email address will not be published. Required fields are marked *