globalsign / mgo

The MongoDB driver for Go
Other
1.97k stars 231 forks source link

Unable to get correct Count of Documents in Collection #330

Closed metalperl closed 5 years ago

metalperl commented 5 years ago

Unable to get a count on Collections in MongoDb:

Expected Results: 155799

Actual Results: 0

What version of MongoDB are you using (mongod --version)?

mgo.BuildInfo Version:"3.6.6"

What version of Go are you using (go version)?

go version go1.10.2 darwin/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/<user>/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/<user>/code/go"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/tk/dv3g2jn16nl0b01m6m9ynxvr0000gn/T/go-build071530832=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "strings"

    mgo "github.com/globalsign/mgo"
    //"github.com/globalsign/mgo/bson"
)

type MongoDBkpis struct {
    Server   string
    Port     string
    Database string
    Models   string
    Username string
    Password string
}

var db *mgo.Database

func (m *MongoDBkpis) gatherCollections() error {
    rawConfig, configErr := ioutil.ReadFile("./mongodb.json")
    checkError(configErr)
    var jsonConfig interface{}
    configErr = json.Unmarshal(rawConfig, &jsonConfig)
    checkError(configErr)
    config := jsonConfig.(map[string]interface{})

    Server := config["server"].(string)
    Port := config["port"].(string)
    Database := config["database"].(string) //Database string consists of <database_name?authSource=admin>
    Models := config["models"].([]interface{})
    Username := config["username"].(string)
    Password := config["password"].(string)
    server := "mongodb://" + Username + ":" + Password + "@" + Server + ":" + Port + "/" + Database

    session, err := mgo.Dial(server)
    if err != nil {
        panic(err)
    }
    defer session.Close()
    db := session.DB(Database)
    //var results []interface{}

    for _, modl := range Models {

        model := modl.(string)

        split := strings.Split(model, ",")
        collection := split[0]

        //err = db.C(collection).Find(nil).Select(bson.M{}).All(&results)
        //fmt.Println(len(results))
        coll, _ := db.C(collection).Count()
        fmt.Println(coll)
    }
    return nil
}

func checkError(err error) {
    if err != nil {
        panic(err)
    }
}

var telegraf = (*MongoDBkpis).gatherCollections

func main() {
    m := MongoDBkpis{"Server", "Port", "Database", "Models", "Username", "Password"}
    telegraf(&m)

Can you reproduce the issue on the latest development branch?

Have not tried as of yet

maitesin commented 5 years ago

Hi @metalperl

Can you try to see if the issue can be produced in development?

Best regards, Oscar

eminano commented 5 years ago

Hi @metalperl,

I've tested the count with the provided version of mgo and it works as expected. This doesn't look like an issue with the library but more to do with your mongo setup/code.

I would suggest the following:

Hope this helps, thanks! Esther