| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 | package moimport (	"context"	"time"	"go.mongodb.org/mongo-driver/bson"	"go.mongodb.org/mongo-driver/bson/primitive"	"go.mongodb.org/mongo-driver/mongo"	"go.mongodb.org/mongo-driver/mongo/options")func NewObjectID() ObjectID {	return primitive.NewObjectID()}func ObjectIDFromHex(s string) (ObjectID, error) {	oid, err := primitive.ObjectIDFromHex(s)	if err != nil {		return NilObjectID, err	}	if oid.IsZero() {		return NilObjectID, primitive.ErrInvalidHex	}	return oid, nil}func ObjectIdMustFromHex(s string) ObjectID {	oid, err := ObjectIDFromHex(s)	if err != nil {		panic(err)	}	return oid}func IsValidObjectID(s string) bool {	_, err := ObjectIDFromHex(s)	return err == nil}// UnmarshalExtJSON 将 json 字符串解析为 bson 类型// data 为字符串字节, canonical 是否为严格类型, val 需要绑定的类型// 可参考 https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/#examples// 与 json.Unmarshal 不同的是: 当 val 为 D / M 时, 会保留 key 的顺序. 但由于 Go 语言 for 循环 map 时会打乱顺序, 因此如果对 key 的顺序// 有要求时请使用 D 作为绑定类型// 用法参见 TestUnmarshalExtJSONfunc UnmarshalExtJSON(data []byte, canonical bool, val interface{}) error {	return bson.UnmarshalExtJSON(data, canonical, val)}func MarshalExtJSON(val any, canonical, escapeHTML bool) ([]byte, error) {	return bson.MarshalExtJSON(val, canonical, escapeHTML)}func NewDateTimeFromTime(t time.Time) DateTime {	return primitive.NewDateTimeFromTime(t)}func NewDecimal128(h, l uint64) Decimal128 {	return primitive.NewDecimal128(h, l)}func IsDuplicateKeyError(err error) bool {	return mongo.IsDuplicateKeyError(err)}func OptionFind() *FindOptions {	return options.Find()}func OptionFindOne() *FindOneOptions {	return options.FindOne()}func OptionFindOneAndUpdate() *FindOneAndUpdateOptions {	return options.FindOneAndUpdate()}func OptionFindOneAndDeleteOptions() *FindOneAndDeleteOptions {	return options.FindOneAndDelete()}func OptionsAggregateOptions() *AggregateOptions {	return options.Aggregate()}func OptionCount() *CountOptions {	return options.Count()}// ResolveIndexName 从 cursor 中解析出索引名称, 索引名称见 IndexName// bool 表示 uniquefunc ResolveIndexName(cursor *Cursor) map[string]bool {	idxMap := make(map[string]bool)	ctx, cancel := context.WithTimeout(context.Background(), DefaultTimout)	defer func() {		_ = cursor.Close(ctx)		cancel()	}()	for cursor.Next(ctx) {		var now M		if err := cursor.Decode(&now); err != nil {			panic(err)		}		var unique bool		if v, ok := now["unique"].(bool); ok {			unique = v		}		idxMap[now["name"].(string)] = unique	}	return idxMap}
 |