Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose with CosmosDB: Getting error `Shared throughput collection should have a partition key`

I have a node-express application that currently uses Mongoose to connect to MongoDB, and am attempting to migrate it to Azure Cosmos DB.

When I simply allow Mongoose to create the database, the application works fine, however the database is created with individual collection RU pricing.

If I create a new database with Shared throughput enabled and attempt to use that, I get the error Shared throughput collection should have a partition key

I have tried updating the collection schema to include a shard key like this:

const mongoose = require('mongoose');

module.exports = function() {
  const types = mongoose.Schema.Types;
  const messages = new mongoose.Schema({
    order: { type: types.ObjectId, required: true, ref: 'orders' },
    createdAt: { type: Date, default: Date.now },
    sender: { type: types.ObjectId, required: true, ref: 'users' },
    recipient: { type: types.ObjectId, ref: 'users' },
    text: { type: String, required: true },
    seen: { type: Boolean, default: false },
  }, { shardKey: { order: 1 } });

  return mongoose.model('messages', messages);
};

However this does not work.

Any ideas on how to create/use a partition key? Alternatively the database is small, so if its possible to remove the requirement for the partition key that would also be fine.

like image 257
alt Avatar asked Dec 10 '18 12:12

alt


People also ask

What is Cosmos DB partition key?

Partition keys are the core element to distributing your data efficiently into different logical and physical sets so that the queries performed against the database are completed as quickly as possible.

Why do we need partition key in cosmos?

Azure Cosmos DB is highly scalable And that is because it uses a partitioning system to scale, which consists of physical and logical partitions. To optimize the scalability and performance of Azure Cosmos DB, you need to choose the right partition key for your container.

Which unit is used to measure throughput in Cosmos DB?

The throughput in cosmos DB is measured using the term Request Unit per sec (RU\sec). We can provision throughput in a form of RU\sec for a specific container or a cosmos database.

Can we update partition key in Cosmos DB?

We can't change the partition key for this container anymore. You can only set it when you create a new container. If you do need to change a partition key, you need to create a new container and migrate your data to that one. A partition key consists of a path.


1 Answers

Now I don't have an exact answer for this question so no need to accept this unless you feel it's correct.

The best solution I've found so far is that this is due to "Provision Throughput" being checked when the database is created in the Azure console. If you delete and recreate the database with this box not checked (it's right below the input for the database name) then you should no longer encounter this error.

like image 53
Nolski Avatar answered Oct 10 '22 05:10

Nolski