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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With