【发布时间】:2021-05-20 14:43:48
【问题描述】:
我有一张地图,它是从一段字符串中创建的。然后我想将它编组为 bson 格式,作为索引插入到 mongodb 中。但是,由于在 Golang 中创建地图的方式,我每次都会得到不同的索引顺序(有时是 abc,有时是 bac、cba...)。
如何确保创建的编组索引始终保持相同的顺序?
fields := ["a", "b", "c"]
compoundIndex := make(map[string]int)
for _, field := range fields {
compoundIndex[field] = 1
}
data, err := bson.Marshal(compoundIndex)
fmt.Println(string(data)) // This output is always in a different order than the desired abc
【问题讨论】:
标签: go mongodb-indexes