Index from One Document to Query Another?

My current data model is as follows (relevant fields only):

// User Collection
{
  _id,
  employeeInfo: [{
    employeeId, 
    businessId, <-- From Business Collection
    roles: [],
    locations: ["lat, lng", ...],
  }],
}

// Business Collection
{
 _id,
...
}

My application will show the businesses a user owns or works for (employeeInfo) along with their role (position) when they sign in, allowing them to select which business to operate the application from. I’m guessing I need to index the businessId field for a user_businesses index, however don’t I need to query the Businesses collection to return a list of them?

How would I go about accomplishing this? Should I modify my existing model? Currently it seems like I’d need to do multiple queries, first on Users, then on Businesses. How can I avoid this? Apologies if my understanding isn’t quite clear. Any additional learning material is greatly appreciated! :blush:

Hello @Andrew_W, here are some points.

Don’t I need to query the Businesses collection to return a list of them?

Not necessarily. The user collection, in addition to id, can also store the business name. This will work fine as the business names change rarely (and you will be storing business name in both collections).

If you think the business names are going to change often or business name cannot be included in users collection, then you need to query the business collection to get the business name - this can be done using a single Aggregation $lookup query (a “join” operation). In such a case only the business id is stored in the user collection. Note that lookup queries generally perform slower than that of a single collection query.


I’m guessing I need to index the businessId field for a user_businesses index

If you are querying on the business id field, an index will be useful in terms of query performance. Indexes on array fields are called as Multikey Indexes.

1 Like

Thank you @Prasad_Saya – This certainly gives me something to think about. I’ll do some tests and see where it gets me. I’m guessing, then, that on the creation of a new business, I’ll need to do an two insert calls on both the user and business collection. This is OK? Seems like it’s the only way to make sure records are synced.

I think that a given business may or may not be associated with a user. So, when a user is created then the existing business’s id and name are included for that user. In case its a new business, then both collections are affected.

And, dont forget that when you make an update of the business name, both collections need to be updated.

1 Like

Data modeling (or data design) plays an important part of designing an application. So do the indexes defined on the collections. Both aspects can affect the performance and maintainability of an application. These topics are vast, in general, but I cannot tell what specific topics you are looking for. The links below are documentation links:

At the top of this page (if you press the Home button on your keyboard you will get there :wink:) there is a Menu, which has links to various resources, including videos, blog posts, etc, and you can search for any specific topics within. You can also search for “mongodb blog posts data models” and “mongodb blog posts indexes or performance” in the Google search box, you will find many useful links.

Finally, MongoDB University has free online classes both for Data Modeling and Performance (and Indexes). It is another way to enhance your knowledge in the respective subjects.

1 Like