Friday, 18 January 2019

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.



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

No comments:

Post a Comment