Home > Software engineering >  Issues running Find().All() using go mongo-driver
Issues running Find().All() using go mongo-driver

Time:10-31

I'm new to mongoDB , Currently we are trying to migrate our old mgo driver to go mongo-driver

In our old code we use something like below from global sign mgo driver

//where apps is a struct
apps := []model.App{}
err = mgo.Collection.Find(query).Skip(skipCount).Limit(MaxResults).Sort("-starttime").All(&apps)

so with the new mongo-driver I tried something like below using Find but that did not work .

    // Set FindOneOptions
    findOpt := options.Find()
    findOpt.SetSkip(int64(skipCount))
    limitVal := appsbody.MaxResults
    findOpt.SetLimit(int64(limitVal))
    findOpt.SetSort("-starttime")

    err = mgo.Collection.Find(query, findOpt).All(context.TODO(), &apps)

In the Above snippet params query is of type map[string]interface{}.
when I tried to log the query Key = type Value = dbuser both are of type string The query value is originally passed by using query := url.Values{}, this case the query type will be map[string][]string

I think later that is passed as map[string]interface{} not sure if that is causing this problem & not able to blend with the right format for query params , So I even tried to converting that using below code , still that did not help me resolve the problem .

    
//do a type conversion for the original query    
    q := make(map[string]interface{}, len(query))
    for key, value := range query {
        q[key] = value
    }

when I try to run the code it fails to do Find operation & I get the below err & throws nil pointer

cannot transform type string to a BSON Document: WriteString can only write while positioned on a Element or Value but is positioned on a TopLevel
panic: runtime error: invalid memory address or nil pointer dereference
        panic: runtime error: invalid memory address or nil pointer dereference
        panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x564171020634]

goroutine 1 [running]:
go.mongodb.org/mongo-driver/mongo.(*Cursor).closeImplicitSession(0x5641708ab4e0?)
        go.mongodb.org/[email protected]/mongo/cursor.go:309  0x14
panic({0x5641716c9440, 0x56417200c2d0})
        runtime/panic.go:884  0x212
go.mongodb.org/mongo-driver/mongo.(*Cursor).Close(0xa1?, {0x5641718f9c30?, 0xc00003a078?})
        go.mongodb.org/[email protected]/mongo/cursor.go:222  0x5f
panic({0x5641716c9440, 0x56417200c2d0})
        runtime/panic.go:884  0x212
go.mongodb.org/mongo-driver/mongo.(*Cursor).All(0x0, {0x5641718f9c30, 0xc00003a078}, {0x5641715fa1c0?, 0xc0001d6480?})
        go.mongodb.org/[email protected]/mongo/cursor.go:251  0x1ff
cr/dev/usvcs/apps/appDBAccess.DbService.GetApps({{0x5641718fda98, 0xc000484960}, {0x5641718f7228, 0xc000012750}, {0x5641718fce68, 0xc000014030}, {0x564171901cb8, 0x5641720bc5d8}}, 0xc0001dbbf0, {0x0, ...})

Not sure what mistake am i doing here , Can anyone help me with this ?

CodePudding user response:

The problem is with the sort value. It must be a document, not a simple string. It may be a map, a bson.M (it's also a map) or a bson.D value (or any other values that marshals "nicely" into BSON, e.g. a struct).

If you only use a single field to sort by, simplest is a bson.M. Also note that method calls on options can be chained (they return the receiver):

findOpt := options.Find().
    SetSkip(int64(skipCount)).
    SetLimit(int64(appsbody.MaxResults)).
    SetSort(bson.M{"starttime": -1})

If you'd have multiple sort keys, order does matter, in which case use a bson.D document (maps are unordered, bson.D is an ordered list of key-value pairs):

findOpt := options.Find().
    SetSkip(int64(skipCount)).
    SetLimit(int64(appsbody.MaxResults)).
    SetSort(bson.D{{Key:"starttime", Value: -1}, {Key:"other", Value: 1}})
  • Related