Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

c# - ComosDB - MongoAPI - Document does not contain shard key

I am investigating using CosmosDB (previously DocumentDB), we currently use MongoDB so I am trying to use the MongoAPI for CosmosDB.

I have created a CosmosDB deployment in azure, created a collection and specified a partition key of "/rateId".

As far as I can understand from Microsofts documentation this partition key should relate to a property in each document I insert, so I am trying to insert a basic document like so:

{
    "rateId": "test.1",
    "val": "test2"
}

However when I try to insert this (through Mongo C# driver or through MongoChef) I get an error "document does not contain shard key".

enter image description here

I have tried this every which way I can think of and every time I am denied with this error. Am I misunderstanding how this is meant to work, or doing something wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In the documentation Microsoft say to use this command for creating a collection through the mongo shell

db.runCommand( { shardCollection: "myDb.myCollection", key: { rateId: "hashed" } } )

I used that to create a collection and it now works as expected (docs with a rateId property insert ok, without I get the "no shard key" error).

When looking at the collection in the Azure Portal it shows the shard key as

$v.rateId.$v

Whereas when I created the collection through the portal and specified /rateId as the partition, it showed it as just

rateId

At least I can progress now, but I'm confused why it behaves this way or if this is how it's meant to be (I can't see any mention of this "$v" format on the documentation)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...