ComosDB-MongoAPI-文档不包含分片键 [英] ComosDB - MongoAPI - Document does not contain shard key

查看:109
本文介绍了ComosDB-MongoAPI-文档不包含分片键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CosmosDB(以前是DocumentDB)进行调查,我们目前使用的是MongoDB,所以我试图将MongoAPI用于CosmosDB.

我已经在azure中创建了一个CosmosDB部署,创建了一个集合并指定了分区密钥"/rateId".

据我从 Microsoft文档此分区键应与我插入的每个文档中的一个属性相关,因此我尝试插入一个基本文档,如下所示:

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

但是,当我尝试通过Mongo C#驱动程序或MongoChef插入此文件时,出现错误文档不包含分片键".

我已经尝试过每一种可以想到的方式,并且每次我都被拒绝此错误时都尝试过.我是在误解这是怎么工作的,还是做错了什么?

解决方案

我用它来创建一个集合,它现在可以按预期的方式工作(具有rateId属性的文档可以正常插入,而不会出现无分片键"错误).

在Azure门户中查看集合时,其碎片键显示为

$v.rateId.$v

当我通过门户网站创建集合并将/rateId指定为分区时,它显示为公正

rateId

至少我现在可以进步,但是我很困惑为什么它会这样运行,或者这就是它的原意(我在文档中看不到这种"$ v"格式的内容)

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".

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?

解决方案

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)

这篇关于ComosDB-MongoAPI-文档不包含分片键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆