创建索引导致未经授权的错误
Creating an index causes unauthorized error
我正在处理一个使用连接到 Azure CosmosDB 的 Go 微服务的项目。
在开发/阶段环境中,我使用的是 MongoDB API 3.6,用于生产 4.0。
微服务在集合上创建索引。对于开发/阶段环境,一切正常。但是在生产中我检索到以下错误:
(Unauthorized) Error=13, Details='Response status code does not
indicate success, Number of regions attempted:1
我已经检查了两次连接字符串,目前生产数据库没有防火墙规则。
我的代码看起来很熟悉:
package repository
import (
"go.mongodb.org/mongo-driver/mongo"
"log"
)
func Collection(db *mongo.Database, c string, indices ...mongo.IndexModel) *mongo.Collection {
col := db.Collection(c)
if indices != nil {
_, err := col.Indexes().CreateMany(ctx, indices)
if err != nil {
log.Fatal(err)
}
}
return col
}
// .....
package service
import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"repository"
)
col := repository.Collection(db, "my_col", []mongo.IndexModel{
{
Keys: bson.M{"uuid": 1},
Options: options.Index().SetUnique(true),
},
}...)
任何人都知道导致此错误的原因吗?
我已经联系了 Microsoft 支持,他们是这样回复的:
This is a limitation of accounts with Point in Time Restore. The collection must be created with a unique index.
https://docs.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:
db.runCommand({
customAction: "CreateCollection",
collection: "my_collection",
shardKey: "my_shard_key",
offerThroughput: 100,
indexes: [{key: {_id: 1}, name: "_id_1"}, {key: {a: 1, b: 1}, name:"a_1_b_1", unique: true} ]
})
所以现在我的代码如下所示:
func Collection(db *mongo.Database, c string, indices []bson.M) *mongo.Collection {
ctx, cls := context.WithTimeout(context.Background(), time.Second * 15)
defer cls()
if cursor, _ := db.ListCollectionNames(ctx, bson.M{"name": c}); len(cursor) < 1 {
cmd := bson.D{{"customAction", "CreateCollection"}, {"collection", c}}
if indices != nil {
cmd = append(cmd, bson.E{Key: "indexes", Value: indices})
}
res := db.RunCommand(ctx, cmd)
if res.Err() != nil {
log.Fatal(res.Err())
}
}
return db.Collection(c)
}
我正在处理一个使用连接到 Azure CosmosDB 的 Go 微服务的项目。 在开发/阶段环境中,我使用的是 MongoDB API 3.6,用于生产 4.0。
微服务在集合上创建索引。对于开发/阶段环境,一切正常。但是在生产中我检索到以下错误:
(Unauthorized) Error=13, Details='Response status code does not indicate success, Number of regions attempted:1
我已经检查了两次连接字符串,目前生产数据库没有防火墙规则。
我的代码看起来很熟悉:
package repository
import (
"go.mongodb.org/mongo-driver/mongo"
"log"
)
func Collection(db *mongo.Database, c string, indices ...mongo.IndexModel) *mongo.Collection {
col := db.Collection(c)
if indices != nil {
_, err := col.Indexes().CreateMany(ctx, indices)
if err != nil {
log.Fatal(err)
}
}
return col
}
// .....
package service
import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"repository"
)
col := repository.Collection(db, "my_col", []mongo.IndexModel{
{
Keys: bson.M{"uuid": 1},
Options: options.Index().SetUnique(true),
},
}...)
任何人都知道导致此错误的原因吗?
我已经联系了 Microsoft 支持,他们是这样回复的:
This is a limitation of accounts with Point in Time Restore. The collection must be created with a unique index.
https://docs.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:
db.runCommand({
customAction: "CreateCollection",
collection: "my_collection",
shardKey: "my_shard_key",
offerThroughput: 100,
indexes: [{key: {_id: 1}, name: "_id_1"}, {key: {a: 1, b: 1}, name:"a_1_b_1", unique: true} ]
})
所以现在我的代码如下所示:
func Collection(db *mongo.Database, c string, indices []bson.M) *mongo.Collection {
ctx, cls := context.WithTimeout(context.Background(), time.Second * 15)
defer cls()
if cursor, _ := db.ListCollectionNames(ctx, bson.M{"name": c}); len(cursor) < 1 {
cmd := bson.D{{"customAction", "CreateCollection"}, {"collection", c}}
if indices != nil {
cmd = append(cmd, bson.E{Key: "indexes", Value: indices})
}
res := db.RunCommand(ctx, cmd)
if res.Err() != nil {
log.Fatal(res.Err())
}
}
return db.Collection(c)
}