Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing different document types in one DocumentDb collection

We have a multitenant application that uses Azure DocumentDB as our NoSQL document oriented database.

For multitenancy, we read this question and this blog post. Because right now our number of users do not meet the need to use different databases and/or documentCollections, and more importantly, for cost savings we implemented multitenancy with a "Where" clause on a TenantId field with one documentCollection.

Similarly, when it comes to storing "documents" or "objects" with complete different natures (say for example Book and Car) we are questioning ourselves on the fact to use one documentCollection.

At first, it looks more reasonable to create two different documentCollection for Book and Car. However, creating a documentCollection costs 25$ minimum. We do not want to pay +25$ everytime we need to add a new feature even if it is to store a low amount of data (e.g. our app stores a lot of Books but few Cars...).

Is it a good design to put Book and Car in the same documentCollection? And keep a reference to the type of the document in a shared member (e.g. string Type ="Book" or string Type = "Car").

Knowing that we have already implemented the Multitenancy with a Where "clause", to query all Cars in our App for a given tenant, our queries would all contain Where TenantId ="XXXX" AND Type = "Car".

I have seen that DocumentDB supports now the Partitioned Collection. Could this be a good usage of the partitions or, on the contrary, they should be kept to achieve better scalability and are not adapted to segregate different document types whose object quantities may not be similar?

like image 458
Benoit Patra Avatar asked Apr 20 '16 16:04

Benoit Patra


People also ask

What is the collection in document database model?

Databases are sets of collections. Collections store records, which are referred to as documents. Collections are the equivalent of tables in RDBMS, and documents can be thought of as rows in a table. The difference is that you don't define what columns (or rather attributes) there will be in advance.

What is Upsert in Cosmos DB?

Upserts a Document as an asychronous operation in the Azure Cosmos DB service. UpsertDocumentAsync(Uri, Object, RequestOptions, Boolean, CancellationToken) Upserts a document as an asynchronous operation in the Azure Cosmos DB service.

Is Cosmos DB a DocumentDB?

Azure Cosmos DB is the next big leap in globally distributed, at scale, cloud databases. As a DocumentDB customer, you now have access to the new breakthrough system and capabilities offered by Azure Cosmos DB.


Video Answer


1 Answers

Yes, it is "good design" to use type="Book". You can also do isBook=true, which I believe is slightly more efficient and enables inheritance and mixin behavior.

Partitioned Collections are actually a way to put more stuff into a single larger entity rather than the other way around. The idea is to allow scaling of both throughput (RUs) and space without the burden of managing multiple Collections yourself. You "could" make your partition key be your type field, but I would not recommend it. Partition keys should enable roughly even spread among partitions... among other criteria.

like image 134
Larry Maccherone Avatar answered Oct 03 '22 22:10

Larry Maccherone