Akshay
Akshay

Reputation: 2763

Shard key and Azure CosmosDB for MongoDB API

I created a CosmosDB database that uses MongoDB's Python SDK, let's call the database as school and the collection as students. While creating the database, the Azure UI asked me to create a shard key for unlimited storage capacity, let's call it school.students.

I am using pymongo to add the following document:

{
  "id": "abc123",
  "name": "jack"
}

and the code as:

from pymongo import MongoClient

student_data = {
  "id": "abc123",
  "name": "jack"
}

client = MongoClient("mongodb://...")
db = client["school"]
collection = db["students"]
collection.insert_one(student_data)

When I chose the storage with 10GB, I have no problem adding the document, but when I chose the unlimited option with shard key, I get the following error:

pymongo.errors.WriteError: document does not contain shard key at 'school.students'

Question is, where should I use the shard key? and how to get over this?

Upvotes: 4

Views: 12344

Answers (2)

David Makogon
David Makogon

Reputation: 71035

A shard key in MongoDB maps to a partition key in Cosmos DB. When you create your collection, you're asked to specify a partition key, if you create it via the portal. Or you can specify the shard key via MongoDB's command line tool or via code (where it's then called a shard key). That shard key must exist in your document.

In your case, your document only contains id and name (and you can use either of those as your shard key). Alternatively, you can specify an additional property as your shard key. For example, maybe schoolid:

student_data = {
  "id": "abc123",
  "name": "jack",
  "schoolid": "school1" 
}

When creating your collection, you'd specify schoolid as your partition key. At this point, your document-save would work, as you will have a shard key (partition key) included in your document.

In your original example, you specified school.students as your shard key. That would require you having a property called school with a sub-property called students (which I'm guessing doesn't make too much sense to do):

student_data = {
  "id": "abc123",
  "name": "jack",
  "school": {
     "students": ...
  }
}

Upvotes: 6

Sajeetharan
Sajeetharan

Reputation: 222552

You can create the collection via the mongo shell

db.runCommand( { shardCollection: "myDb.students", key: { id: "hashed" } } )

Upvotes: 1

Related Questions