| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 | package moimport (	"encoding/xml"	"fmt"	"time")type Type int8// https://docs.mongodb.com/manual/reference/bson-types/const (	TypeDouble     Type = 1   // float64	TypeString     Type = 2   // string	TypeObject     Type = 3   // M	TypeArray      Type = 4   // A	TypeBinData    Type = 5   // Binary reference https://bsonspec.org/spec.html subtype	TypeObjectId   Type = 7   // ObjectID	TypeBoolean    Type = 8   // bool	TypeDate       Type = 9   // DateTime	TypeNull       Type = 10  // nil	TypeRegex      Type = 11  // Regex	TypeJavaScript Type = 13  // JavaScript	TypeInt        Type = 16  // int32	TypeTimestamp  Type = 17  // Timestamp DO NOT USE, for internal MongoDB only: https://docs.mongodb.com/manual/reference/bson-types/#timestamps	TypeLong       Type = 18  // int64	TypeDecimal128 Type = 19  // Decimal128	TypeMinKey     Type = -1  // MinKey	TypeMaxKey     Type = 127 // MaxKey	TypeFloat64 = TypeDouble // alias	TypeMap     = TypeObject	TypeSlice   = TypeArray	TypeBool    = TypeBoolean	TypeInt32   = TypeInt	TypeInt64   = TypeLong	TypeBinary  = TypeBinData)var nameType = map[Type]string{	TypeDouble:     "double",	TypeString:     "string",	TypeObject:     "object",	TypeArray:      "array",	TypeBinData:    "binData",	TypeObjectId:   "objectId",	TypeBoolean:    "bool",	TypeDate:       "date",	TypeNull:       "null",	TypeRegex:      "regex",	TypeJavaScript: "javascript",	TypeInt:        "int",	TypeTimestamp:  "timestamp",	TypeLong:       "long",	TypeDecimal128: "decimal",	TypeMinKey:     "minKey",	TypeMaxKey:     "maxKey",}var typeName = map[string]Type{	"double":     TypeDouble,	"string":     TypeString,	"object":     TypeObject,	"array":      TypeArray,	"binData":    TypeBinData,	"objectId":   TypeObjectId,	"bool":       TypeBoolean,	"date":       TypeDate,	"null":       TypeNull,	"regex":      TypeRegex,	"javascript": TypeJavaScript,	"int":        TypeInt,	"timestamp":  TypeTimestamp,	"long":       TypeLong,	"decimal":    TypeDecimal128,	"minKey":     TypeMinKey,	"maxKey":     TypeMaxKey,	// alias	"float64": TypeDouble,	"float":   TypeDouble,	"map":     TypeObject,	"slice":   TypeArray,	"binary":  TypeBinData,	"int32":   TypeInt,	"int64":   TypeLong,}func (t *Type) UnmarshalXMLAttr(attr xml.Attr) error {	if v, ok := typeName[attr.Value]; ok {		*t = v		return nil	}	return fmt.Errorf("unknown mo.Type(%s)", attr.Value)}func (t *Type) String() string {	if v, ok := nameType[*t]; ok {		return fmt.Sprintf("mo.Type(%s)", v)	}	return fmt.Sprintf("mo.Type(%d)", t)}func (t *Type) Default() any {	switch *t {	case TypeDouble:		return float64(0)	case TypeString:		return ""	case TypeObject:		return M{}	case TypeArray:		return A{}	case TypeBinData:		return Binary{}	case TypeObjectId:		return NilObjectID	case TypeBoolean:		return false	case TypeDate:		return DateTime(0)	case TypeNull:		return nil	case TypeRegex:		return Regex{}	case TypeJavaScript:		return JavaScript("")	case TypeInt:		return int32(0)	case TypeTimestamp:		return Timestamp{}	case TypeLong:		return int64(0)	case TypeDecimal128:		return NewDecimal128(0, 0)	case TypeMinKey:		return MinKey{}	case TypeMaxKey:		return MaxKey{}	default:		return nil	}}const (	DefaultTimout = 10 * time.Second)const (	DefaultDbName = "test"	// ISODate 作为 DateTime 字符串时间模板, 来自 time.RFC3339 增加毫秒, 见 time/format.go:96	ISODate = "2006-01-02T15:04:05.000Z07:00")// https://www.mongodb.com/docs/v6.0/reference/operator/aggregation-pipeline/#aggregation-pipeline-stagesconst (	PsMatch     = "$match"	PsLookup    = "$lookup"	PsProject   = "$project"	PsGroup     = "$group"	PsSort      = "$sort"	PsLimit     = "$limit"	PsSkip      = "$skip"	PsSet       = "$set"	PsDocuments = "$documents")// https://www.mongodb.com/docs/v6.0/reference/operator/aggregation/#aggregation-pipeline-operatorsconst (	PoAdd = "$add"	PoSum = "$sum")const (	PoCurrentDate = "$currentDate")const (	PoSet = "$set")const (	PoPush    = "$push"	PoPull    = "$pull"	PoPullAll = "$pullAll")
 |