IndexStats Query in DocumentDB returning 0 ops - mongodb

I'm trying to build a Documentdb Prometheus exporter. I'm trying to get the IndexStats to identify the unused Indices. But I'm getting ops value as 0 even though the ops value is not 0. Can you please help me identify the error?
This the Go code for the same.
databases, err := client.ListDatabaseNames(ctx, bson.D{})
if err != nil{
return make(map[string]map[string]interface{}), err
}
var indexMap map[string]map[string]interface{} = make(map[string]map[string]interface{})
for _, database := range databases {
collections, err := client.Database(database).ListCollectionNames(ctx, bson.D{})
if err != nil {
return make(map[string]map[string]interface{}), err
}
for _, collectionName := range collections {
collection := client.Database(database).Collection(collectionName)
indexStats := bson.D{{Key: "$indexStats", Value: bson.M{}}}
cursor, err := collection.Aggregate(ctx, mongo.Pipeline{indexStats})
if err != nil {
return make(map[string]map[string]interface{}), err
}
var indices []bson.M
err = cursor.All(ctx, &indices)
if err != nil {
return make(map[string]map[string]interface{}), err
}
err = cursor.Close(ctx)
if err != nil {
return make(map[string]map[string]interface{}), err
}
fmt.Print(database + " " + collectionName + " ")
fmt.Println(indices)
indicesBytes, err := json.Marshal(indices)
if err != nil {
return make(map[string]map[string]interface{}), err
}
var indicesJson []interface{}
err = json.Unmarshal(indicesBytes, &indicesJson)
if err != nil {
return make(map[string]map[string]interface{}), err
}
if _, ok := indexMap[database]; !ok {
indexMap[database] = make(map[string]interface{})
}
indexMap[database][collectionName] = indicesJson
}
}
May I know the reason and the fix for this issue?
Thanks in Advance :)

Related

session was not created by this client - MongoDB, Golang

I am trying to creat transaction in MongoDB with Golang and Iris. Problem is that transaction did not accept iris context and Con, I don't know why this thing happened. Can you tell me what I am doing wrong here?
Main.go Using Iris
func main() {
app := iris.New()
app.Logger().SetLevel("debug")
app.Use(recover.New())
app.Use(logger.New())
// Resource: http://localhost:8080
app.Get("/", func(ctx iris.Context) {
ctx.JSON(iris.Map{"message": "Welcome to Woft Bank"})
})
// API endpoints
router.SetRoute(app)
app.Listen(PORT)}
Router
func SetRoute(app *iris.Application) {
userRoute := app.Party("/user")
{
userRoute.Post("/register", middleware.UserValidator, controller.CreateUser)
userRoute.Get("/detail", middleware.UserValidator, controller.GetUserBalanceWithUserID)
userRoute.Patch("/transfer", middleware.TransferValidator, controller.Transfer)
}}
Transacion function (session was not created by this client)
func Transfer(ctx iris.Context) {
senderID := ctx.URLParam("from")
receiverID := ctx.URLParam("to")
amount, _ := strconv.ParseInt(ctx.URLParam("amount"), 10, 64)
session, err := Config.DB().StartSession()
if err != nil {
handleErr(ctx, err)
return
}
defer session.EndSession(ctx)
callback := func(sessCtx mongo.SessionContext) (interface{}, error) {
upsert := false
after := options.After
opt := options.FindOneAndUpdateOptions{
ReturnDocument: &after,
Upsert: &upsert,
}
sender := Models.User{}
filter := bson.M{"username": senderID}
update := bson.M{"$inc": bson.M{"balance": -amount}}
//FindOneAndUpdate did not accept sessCtx
err := UserCollection.FindOneAndUpdate(sessCtx, filter, update, &opt).Decode(&sender)
if err != nil {
return nil, err
}
if sender.Balance < 0 {
return nil, errors.New("sender's balance is not enough")
}
filter = bson.M{"username": receiverID}
update = bson.M{"$inc": bson.M{"balance": +amount}}
_, err = UserCollection.UpdateOne(sessCtx, filter, update)
if err != nil {
return nil, err
}
return sender, nil
}
result, err := session.WithTransaction(ctx, callback)
if err != nil {
handleErr(ctx, err)
return
}
response(result, "success", ctx)}

How to do pagination with MongoDB

I need pagination for Go and MongoDB and I'm highly frustrated with this problem since I cant achieve pagination since weeks now! I dont want to use "Skip" as it is too much resources consuming.
Here is the code:
var limit int64 = 20
var offset int64 = 40
findOptions0 := options.Find()
findOptions0.SetLimit(offset)
findOptions0.SetSort(bson.M{"_id": 1})
findOptions := options.Find()
findOptions.SetLimit(limit)
findOptions.SetSort(bson.M{"_id": 1})
cursor0, err32 := collection.Find(context.Background(), bson.M{}, findOptions)
if err32 != nil {
return status.Errorf(codes.Internal, fmt.Sprintf("Unknown1 internal error: %v", err32))
}
for cursor0.Next(context.Background()) {
err := cursor0.Decode(&data0)
if err != nil {
return status.Errorf(codes.Unavailable, fmt.Sprintf("Could not decode data of cursor0: %v", err))
}
}
err := cursor0.Decode(&data0)
if err != nil {
return status.Errorf(codes.Unavailable, fmt.Sprintf("Could not decode data of cursor0: %v", err))
}
cursor, err34 := collection.Find(context.Background(), bson.M{"_id": bson.M{"$gt": data0.ID}}, findOptions)
if err34 != nil {
log.Printf("lastID: ", data0.ID)
return status.Errorf(codes.Internal, fmt.Sprintf("Unknown2 internal error: %v", err34))
}
for cursor.Next(context.Background()) {
log.Printf("result: ", cursor)
// Decode the data at the current pointer and write it to data
err := cursor.Decode(data)
// check error
if err != nil {
return status.Errorf(codes.Unavailable, fmt.Sprintf("Could not decode data: %v", err))
}
log.Printf("End of the for loop ")

Deserialize cursor into array with mongo-go-driver and interface

I create an api using golang, i would like to create some functionnal test, for that i create an interface to abstract my database. But for that i need to be able to convert the cursor to an array without knowing the type.
func (self *KeyController) GetKey(c echo.Context) (err error) {
var res []dto.Key
err = db.Keys.Find(bson.M{}, 10, 0, &res)
if err != nil {
fmt.Println(err)
return c.String(http.StatusInternalServerError, "internal error")
}
c.JSON(http.StatusOK, res)
return
}
//THE FIND FUNCTION ON THE DB PACKAGE
func (s MongoCollection) Find(filter bson.M, limit int, offset int, res interface{}) (err error) {
ctx := context.Background()
var cursor *mongo.Cursor
l := int64(limit)
o := int64(offset)
objectType := reflect.TypeOf(res).Elem()
cursor, err = s.c.Find(ctx, filter, &options.FindOptions{
Limit: &l,
Skip: &o,
})
if err != nil {
return
}
defer cursor.Close(ctx)
for cursor.Next(ctx) {
result := reflect.New(objectType).Interface()
err := cursor.Decode(&result)
if err != nil {
panic(err)
}
res = append(res.([]interface{}), result)
}
return
}
Does someone have an idea?
You can call directly the "All" method:
ctx := context.Background()
err = cursor.All(ctx, res)
if err != nil {
fmt.Println(err.Error())
}
For reference:
https://godoc.org/go.mongodb.org/mongo-driver/mongo#Cursor.All
i think you want to encapsulate the Find method for mongo query.
Using the reflect package i have improved your code by adding an additional parameter that serves as a template to instantiate new instances of slice items.
func (m *MongoDbModel) FindAll(database string, colname string, obj interface{}, parameter map[string]interface{}) ([]interface{}, error) {
var list = make([]interface{}, 0)
collection, err := m.Client.Database(database).Collection(colname).Clone()
objectType := reflect.TypeOf(obj).Elem()
fmt.Println("objectype", objectType)
if err != nil {
log.Println(err)
return nil, err
}
filter := bson.M{}
filter["$and"] = []bson.M{}
for key, value := range parameter {
filter["$and"] = append(filter["$and"].([]bson.M), bson.M{key: value})
}
cur, err := collection.Find(context.Background(), filter)
if err != nil {
log.Fatal(err)
}
defer cur.Close(context.Background())
for cur.Next(context.Background()) {
result := reflect.New(objectType).Interface()
err := cur.Decode(result)
if err != nil {
log.Println(err)
return nil, err
}
list = append(list, result)
}
if err := cur.Err(); err != nil {
return nil, err
}
return list, nil
}
The difference is that FindAll method returns []interface{}, where err := cur.Decode(result) directly consumes a pointer like the result variable.

Example for transactions in mongodb with GoLang

I need an example to implement transactions in MongoDB with GoLang.
I'm using this golang driver for mongodb
https://github.com/mongodb/mongo-go-driver
There is no clear documentation for how to implement transactions.
Can anyone help me?
It can be confusing. Below is a simple example.
if session, err = client.StartSession(); err != nil {
t.Fatal(err)
}
if err = session.StartTransaction(); err != nil {
t.Fatal(err)
}
if err = mongo.WithSession(ctx, session, func(sc mongo.SessionContext) error {
if result, err = collection.UpdateOne(sc, bson.M{"_id": id}, update); err != nil {
t.Fatal(err)
}
if result.MatchedCount != 1 || result.ModifiedCount != 1 {
t.Fatal("replace failed, expected 1 but got", result.MatchedCount)
}
if err = session.CommitTransaction(sc); err != nil {
t.Fatal(err)
}
return nil
}); err != nil {
t.Fatal(err)
}
session.EndSession(ctx)
You can view full examples here.
This will help you
ctx := context.Background()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
panic(err)
}
db := client.Database("testdb")
defer db.Client().Disconnect(ctx)
col := db.Collection("testcol")
// transaction
err = db.Client().UseSession(ctx, func(sessionContext mongo.SessionContext) error {
err := sessionContext.StartTransaction()
if err != nil {
return err
}
_, err = col.InsertOne(sessionContext, bson.M{"_id": "1", "name": "berry"})
if err != nil {
return err
}
_, err = col.InsertOne(sessionContext, bson.M{"_id": "2", "name": "gucci"})
if err != nil {
sessionContext.AbortTransaction(sessionContext)
return err
}
if err = session.CommitTransaction(sessionContext); err != nil {
return err
}
return nil
})

Golang bcrypt hashed passwords dont match

so I am storing a password hash in mongodb in example one below. If I add it then retrieve in the same function it says the password and hash match, however if I try to retrieve it in another function, both values are still the same, except the hashed password does not match the supplied password. Example below.
c := session.DB("users").C("accounts")
err = c.Find(bson.M{"username": user.Username}).One(&userInfo)
if err != nil {
return err
}
log.Println(user.Password+Pepper)
log.Println(userInfo)
err = bcrypt.CompareHashAndPassword(userInfo.PWHash, []byte(user.Password+Pepper))
if err != nil {
return err
}
return nil
The example above the password does not match the hash.
orig := user.Password
user.PWHash, err = bcrypt.GenerateFromPassword([]byte(user.Password+Pepper), bcrypt.DefaultCost)
session, err := mgo.Dial("mongodb")
if err != nil {
return err
}
defer session.Close()
if InTesting {
err = session.DB("users").DropDatabase()
if err != nil {
return err
}
}
c := session.DB("users").C("accounts")
err = c.Insert(user)
if err != nil {
return err
}
var userInfo models.UserAuth
err = c.Find(bson.M{"username": user.Username}).One(&userInfo)
if err != nil {
return err
}
log.Println(orig + Pepper)
log.Println(userInfo)
err = bcrypt.CompareHashAndPassword(userInfo.PWHash, []byte(orig+Pepper))
if err != nil {
log.Println(err)
} else {
log.Println("matched!")
}
In the example above the passwords do match. Both examples the comments of the 2 logs contain identical information. I have no ideas at all. Any help is greatly appreciated :)