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

橙三吉。 提交于 2021-01-29 05:13:44

问题


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 :(


回答1:


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}}


来源:https://stackoverflow.com/questions/64226091/missing-type-in-composite-literal-go-and-missing-key-in-map-literal-go

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!