| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 | package moimport (	"fmt"	"go.mongodb.org/mongo-driver/mongo/options")// NewIndexModel 创建索引// 参考 https://www.mongodb.com/docs/manual/core/index-single/////	https://www.mongodb.com/docs/manual/indexes///// field 为需要创建索引的字段, 而 i 为 1 或 -1 用于索引的字段排序, 即最终的索引名称为 field + 下划线 + i// 例如: field_1// 索引的顺序无关紧要, 参见 https://www.mongodb.com/docs/manual/indexes/#single-field// 为了方便操作, mo 永远将 i 设置为 1// 通常情况下应使用 NewIndex 创建索引func NewIndexModel(filed string, i int32) IndexModel {	return IndexModel{		Keys:    M{filed: i},		Options: options.Index().SetUnique(true), // 设置为唯一值	}}// NewIndex 创建索引func NewIndex(field string) IndexModel {	return NewIndexModel(field, 1)}// NewIndexes 批量创建索引func NewIndexes(field []string) []IndexModel {	index := make([]IndexModel, len(field))	for i := 0; i < len(field); i++ {		index[i] = NewIndex(field[i])	}	return index}// IndexName 索引名称, 将 field 包装为 MongoDB 索引名称// 详情参见 NewIndexModelfunc IndexName(field string) string {	return fmt.Sprintf("%s_1", field)}
 |