Search code examples
mongodbdictionarygomongo-go

missing type in composite literal go AND missing key in map literal go


Im trying to do Pagination with MongoDB

I write this code:

findOptions := options.Find()
    findOptions.SetLimit(20)
    findOptions.SetSort(bson.M{{"_id", 1}})

    cursor, err34 := collection.Find(context.Background(), bson.M{{"_id", bson.M{{"$gte", last_id}}}}, findOptions)

Now It keeps complaining:

missing type in composite literal go AND missing key in map literal go

It complains for this part:

findOptions.SetSort(bson.M{{"_id", 1}})

and

bson.M{{"_id", bson.M{{"$gte", last_id}}}}, findOptions)

I'm stuck with this error since so many hours and its very frustrating.

Please Help :(


Solution

  • bson.M is a map:

    type M map[string]interface{}
    

    So use the map composite literal syntax to create a value of it:

    bson.M{"_id": 1}
    

    And:

    bson.M{"_id": bson.M{"$gte": last_id}}