Store data in a single collection or multiple collections?

Hi there, pretty much I just want to quick as about should it be going with Single collection or Multi collection.

I have this listing collection that has many types. Each type is consisting of different existing fields (indexes), such an example:

Type: Estate, Subtype: House

  • other fields
  • garage (Int) (index)
  • bathroom (Int) (index)
  • bedroom (Int) (index)
  • building_size (Int) (index)
  • land_size (Int) (index)
  • other fields

Type: Estate, Subtype: Land

  • other fields
  • land_size (Int) (index)
  • other fields

Note that, each subtype of type “estate” has different existing indexes fields. Not only “estate”, I’d have “lost” and “found” types on our listing collection, of course, “lost” and “found” would have different existing fields for indexing.

Every type has its own existing indexed fields, with this then, should I go with Single Collection or Multi-collections to differentiate with their type?


Hello @ibgpramana, some factors determine how you structure the data in a collection.

The important ones to consider are the number of documents in the collection (and for each type, in this case) and the kind of queries your application has (these include CRUD (Create, Read, Update and Delete) operations).

The application’s functionality is one of the main criteria in determining how you organize the data. So, what is your application, what kind of functions, how you use the data in the application, etc., is what you need to tell us for further discussion.

That said, MongoDB’s document model allows documents with a flexible schema. So, if it suits your application needs you can have documents with different types within the same collection. But, with this structure, will your application program be able to do the operations on the data without complex logic? Having complex logic in an application means a lot of maintenance overhead (in future).

1 Like

Thanks for reply,

The first factor to consider on base what you’ve stated is: the number of documents in the collection. This is depends on the users, they can post many listing for each type they want, and of course, it involves with CRUD operations.

About what is my application and function, think of a craigslist. All the listing data in the application will be Listing with many types, and each type has their own many subtypes.

I agree that MongoDB’s allows us implement a flexible schema. Currently, we’re in the progress of building this app, and use the Single Collection approach, where listings collection stored and keep many listing types.

The reason why I’m asking this is that, I’m concerned whether my current scenario is the right schema. Note that, Estate (House) would have indexes for garage, bathroom, and bedroom, but Estate (Land) would never have existing fields + indexes for those.

@ibgpramana, there is an index type called a Partial Index. This index allows creating index on specific criteria (or condition). The documentation says:

Partial indexes only index the documents in a collection that meet a specified filter expression. By indexing a subset of the documents in a collection, partial indexes have lower storage requirements and reduced performance costs for index creation and maintenance.

Your data can benefit from this indexing. For example, if the subtype is "Land" you can create a partial index on this condition.