创建索引会导致未经授权的错误。

huangapple go评论126阅读模式
英文:

Creating an index causes unauthorized error

问题

我正在使用Go微服务连接到Azure CosmosDB的项目中工作。
在开发/阶段环境中,我使用的是MongoDB API 3.6,而在生产环境中使用的是4.0。

这些微服务在集合上创建索引。在开发/阶段环境中一切正常。但是在生产环境中,我遇到了以下错误:

> (Unauthorized) Error=13, Details='Response status code does not
> indicate success, Number of regions attempted:1

我已经检查了连接字符串两次,并且当前生产数据库没有防火墙规则。

我的代码类似于这样:

  1. package repository
  2. import (
  3. "go.mongodb.org/mongo-driver/mongo"
  4. "log"
  5. )
  6. func Collection(db *mongo.Database, c string, indices ...mongo.IndexModel) *mongo.Collection {
  7. col := db.Collection(c)
  8. if indices != nil {
  9. _, err := col.Indexes().CreateMany(ctx, indices)
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. }
  14. return col
  15. }
  16. // .....
  17. package service
  18. import (
  19. "go.mongodb.org/mongo-driver/bson"
  20. "go.mongodb.org/mongo-driver/mongo"
  21. "go.mongodb.org/mongo-driver/mongo/options"
  22. "repository"
  23. )
  24. col := repository.Collection(db, "my_col", []mongo.IndexModel{
  25. {
  26. Keys: bson.M{"uuid": 1},
  27. Options: options.Index().SetUnique(true),
  28. },
  29. }...)

有人知道是什么原因导致了这个错误吗?

英文:

I'm working on a project using Go microservices connecting to an Azure CosmosDB.
On the dev / stage environment I'm using the MongoDB API 3.6, for production 4.0.

The microservices creating indices on the collections. For the dev / stage environment all work's fine. But on production I'm retrieving the following error:

> (Unauthorized) Error=13, Details='Response status code does not
> indicate success, Number of regions attempted:1

I've checked the connection string twice and currently there are no firewall rules for the production db.

My code looks familiar to this:

  1. package repository
  2. import (
  3. "go.mongodb.org/mongo-driver/mongo"
  4. "log"
  5. )
  6. func Collection(db *mongo.Database, c string, indices ...mongo.IndexModel) *mongo.Collection {
  7. col := db.Collection(c)
  8. if indices != nil {
  9. _, err := col.Indexes().CreateMany(ctx, indices)
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. }
  14. return col
  15. }
  16. // .....
  17. package service
  18. import (
  19. "go.mongodb.org/mongo-driver/bson"
  20. "go.mongodb.org/mongo-driver/mongo"
  21. "go.mongodb.org/mongo-driver/mongo/options"
  22. "repository"
  23. )
  24. col := repository.Collection(db, "my_col", []mongo.IndexModel{
  25. {
  26. Keys: bson.M{"uuid": 1},
  27. Options: options.Index().SetUnique(true),
  28. },
  29. }...)

Anyone an idea what causes this error?

答案1

得分: 1

我已经联系了Microsoft支持,这是他们的回复:

这是具有时间点恢复功能的帐户的限制。集合必须使用唯一索引创建。

https://learn.microsoft.com/en-us/azure/cosmos-db/continuous-backup-restore-introduction

您可以使用以下命令创建已经存在唯一索引的集合(可以从Mongo shell、Robo3T或其他客户端执行):

MongoDB扩展命令以管理Azure Cosmos DB的MongoDB API中的数据 | Microsoft Docs

例如:

  1. db.runCommand({
  2. customAction: "CreateCollection",
  3. collection: "my_collection",
  4. shardKey: "my_shard_key",
  5. offerThroughput: 100,
  6. indexes: [{key: {_id: 1}, name: "_id_1"}, {key: {a: 1, b: 1}, name:"a_1_b_1", unique: true} ]
  7. })

所以现在我的代码看起来像这样:

  1. func Collection(db *mongo.Database, c string, indices []bson.M) *mongo.Collection {
  2. ctx, cls := context.WithTimeout(context.Background(), time.Second * 15)
  3. defer cls()
  4. if cursor, _ := db.ListCollectionNames(ctx, bson.M{"name": c}); len(cursor) < 1 {
  5. cmd := bson.D{{"customAction", "CreateCollection"}, {"collection", c}}
  6. if indices != nil {
  7. cmd = append(cmd, bson.E{Key: "indexes", Value: indices})
  8. }
  9. res := db.RunCommand(ctx, cmd)
  10. if res.Err() != nil {
  11. log.Fatal(res.Err())
  12. }
  13. }
  14. return db.Collection(c)
  15. }
英文:

I've contacted the Microsoft support and this is what they replied:

> This is a limitation of accounts with Point in Time Restore. The collection must be created with a unique index.
>
> https://learn.microsoft.com/en-us/azure/cosmos-db/continuous-backup-restore-introduction
>
> You can use a command such as this to create the collection with the unique index already present (from the Mongo shell, or Robo3T, or another client)
>
> MongoDB extension commands to manage data in Azure Cosmos DB’s API for MongoDB | Microsoft Docs
>
>For example:

  1. db.runCommand({
  2. customAction: &quot;CreateCollection&quot;,
  3. collection: &quot;my_collection&quot;,
  4. shardKey: &quot;my_shard_key&quot;,
  5. offerThroughput: 100,
  6. indexes: [{key: {_id: 1}, name: &quot;_id_1&quot;}, {key: {a: 1, b: 1}, name:&quot;a_1_b_1&quot;, unique: true} ]
  7. })

So now my code looks like this:

  1. func Collection(db *mongo.Database, c string, indices []bson.M) *mongo.Collection {
  2. ctx, cls := context.WithTimeout(context.Background(), time.Second * 15)
  3. defer cls()
  4. if cursor, _ := db.ListCollectionNames(ctx, bson.M{&quot;name&quot;: c}); len(cursor) &lt; 1 {
  5. cmd := bson.D{{&quot;customAction&quot;, &quot;CreateCollection&quot;}, {&quot;collection&quot;, c}}
  6. if indices != nil {
  7. cmd = append(cmd, bson.E{Key: &quot;indexes&quot;, Value: indices})
  8. }
  9. res := db.RunCommand(ctx, cmd)
  10. if res.Err() != nil {
  11. log.Fatal(res.Err())
  12. }
  13. }
  14. return db.Collection(c)
  15. }

huangapple
  • 本文由 发表于 2021年8月5日 20:08:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/68666316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定