Documentation
¶
Overview ¶
Package mongodb encapsulates standard library mongodb.
Index ¶
- Constants
- Variables
- func IsDuplicateKeyError(err error) bool
- func NewClientTransport(opt ...transport.ClientTransportOption) transport.ClientTransport
- func NewInsertOneModel() *mongo.InsertOneModel
- func NewMongoTransport(opt ...ClientTransportOption) transport.ClientTransport
- func NewReplaceOneModel() *mongo.ReplaceOneModel
- func NewSessionContext(ctx context.Context, sess mongo.Session) mongo.SessionContext
- func NewUpdateManyModel() *mongo.UpdateManyModel
- func NewUpdateOneModel() *mongo.UpdateOneModel
- type Client
- type ClientCodec
- type ClientTransport
- type ClientTransportOption
- type Config
- type IndexViewer
- type Request
- type Response
- type TxFunc
Constants ¶
const (
RetDuplicateKeyErr = 11000 // key conflict error
)
error code, refer: https://github.com/mongodb/mongo/blob/master/src/mongo/base/error_codes.yml
Variables ¶
var ( Find = "find" FindOne = "findone" FindOneAndReplace = "findoneandreplace" FindC = "findc" // Return mongo.Cursor type interface, use cursor.All/Decode to parse to structure. DeleteOne = "deleteone" DeleteMany = "deletemany" FindOneAndDelete = "findoneanddelete" FindOneAndUpdate = "findoneandupdate" FindOneAndUpdateS = "findoneandupdates" // Return mongo.SingleResult type interface, // use Decode to parse to structure. InsertOne = "insertone" InsertMany = "insertmany" UpdateOne = "updateone" UpdateMany = "updatemany" ReplaceOne = "replaceone" Count = "count" Aggregate = "aggregate" // Polymerization AggregateC = "aggregatec" // Return mongo.Cursor type interface, // use cursor.All/Decode to parse to structure. Distinct = "distinct" BulkWrite = "bulkwrite" CountDocuments = "countdocuments" EstimatedDocumentCount = "estimateddocumentcount" Watch = "watch" WatchDatabase = "watchdatabase" WatchCollection = "watchcollection" Transaction = "transaction" Disconnect = "disconnect" RunCommand = "runcommand" // Execute commands sequentially IndexCreateOne = "indexcreateone" // Create index IndexCreateMany = "indexcreatemany" // Create indexes in batches IndexDropOne = "indexdropone" // Delete index IndexDropAll = "indexdropall" // Delete all indexes Indexes = "indexes" // Get the original index object DatabaseCmd = "database" // Get the original database CollectionCmd = "collection" // Get the original collection StartSession = "startsession" // Create a new Session and SessionContext )
mongodb cmd definition
var DefaultClientTransport = NewMongoTransport()
DefaultClientTransport is a default client mongodb transport.
Functions ¶
func IsDuplicateKeyError ¶
IsDuplicateKeyError handles whether it is a key conflict error.
func NewClientTransport ¶
func NewClientTransport(opt ...transport.ClientTransportOption) transport.ClientTransport
NewClientTransport creates a mongodb transport. Deprecated,use NewMongoTransport instead.
func NewInsertOneModel ¶
func NewInsertOneModel() *mongo.InsertOneModel
NewInsertOneModel creates a new InsertOneModel. InsertOneModel is used to insert a single document in a BulkWrite operation.
func NewMongoTransport ¶
func NewMongoTransport(opt ...ClientTransportOption) transport.ClientTransport
NewMongoTransport creates a mongodb transport.
func NewReplaceOneModel ¶
func NewReplaceOneModel() *mongo.ReplaceOneModel
NewReplaceOneModel creates a new ReplaceOneModel. ReplaceOneModel is used to replace at most one document in a BulkWrite operation.
func NewSessionContext ¶
NewSessionContext creates a new SessionContext associated with the given Context and Session parameters.
func NewUpdateManyModel ¶
func NewUpdateManyModel() *mongo.UpdateManyModel
NewUpdateManyModel creates a new UpdateManyModel. UpdateManyModel is used to update multiple documents in a BulkWrite operation.
func NewUpdateOneModel ¶
func NewUpdateOneModel() *mongo.UpdateOneModel
NewUpdateOneModel creates a new UpdateOneModel. UpdateOneModel is used to update at most one document in a BulkWrite operation.
Types ¶
type Client ¶
type Client interface { BulkWrite(ctx context.Context, database string, coll string, models []mongo.WriteModel, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error) InsertOne(ctx context.Context, database string, coll string, document interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error) InsertMany(ctx context.Context, database string, coll string, documents []interface{}, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error) DeleteOne(ctx context.Context, database string, coll string, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) DeleteMany(ctx context.Context, database string, coll string, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) UpdateOne(ctx context.Context, database string, coll string, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) UpdateMany(ctx context.Context, database string, coll string, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) ReplaceOne(ctx context.Context, database string, coll string, filter interface{}, replacement interface{}, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error) Aggregate(ctx context.Context, database string, coll string, pipeline interface{}, opts ...*options.AggregateOptions) (*mongo.Cursor, error) CountDocuments(ctx context.Context, database string, coll string, filter interface{}, opts ...*options.CountOptions) (int64, error) EstimatedDocumentCount(ctx context.Context, database string, coll string, opts ...*options.EstimatedDocumentCountOptions) (int64, error) Distinct(ctx context.Context, database string, coll string, fieldName string, filter interface{}, opts ...*options.DistinctOptions) ([]interface{}, error) Find(ctx context.Context, database string, coll string, filter interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error) FindOne(ctx context.Context, database string, coll string, filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult FindOneAndDelete(ctx context.Context, database string, coll string, filter interface{}, opts ...*options.FindOneAndDeleteOptions) *mongo.SingleResult FindOneAndReplace(ctx context.Context, database string, coll string, filter interface{}, replacement interface{}, opts ...*options.FindOneAndReplaceOptions) *mongo.SingleResult FindOneAndUpdate(ctx context.Context, database string, coll string, filter interface{}, update interface{}, opts ...*options.FindOneAndUpdateOptions) *mongo.SingleResult Watch(ctx context.Context, pipeline interface{}, opts ...*options.ChangeStreamOptions) (*mongo.ChangeStream, error) WatchDatabase(ctx context.Context, database string, pipeline interface{}, opts ...*options.ChangeStreamOptions) (*mongo.ChangeStream, error) WatchCollection(ctx context.Context, database string, collection string, pipeline interface{}, opts ...*options.ChangeStreamOptions) (*mongo.ChangeStream, error) Transaction(ctx context.Context, sf TxFunc, tOpts []*options.TransactionOptions, opts ...*options.SessionOptions) error Disconnect(ctx context.Context) error RunCommand(ctx context.Context, database string, runCommand interface{}, opts ...*options.RunCmdOptions) *mongo.SingleResult Indexes(ctx context.Context, database string, collection string) (mongo.IndexView, error) Database(ctx context.Context, database string) (*mongo.Database, error) Collection(ctx context.Context, database string, collection string) (*mongo.Collection, error) StartSession(ctx context.Context) (mongo.Session, error) //Deprecated Do(ctx context.Context, cmd string, db string, coll string, args map[string]interface{}) (interface{}, error) }
Client is mongodb request interface.
type ClientCodec ¶
type ClientCodec struct{}
ClientCodec decodes mongodb client request.
type ClientTransport ¶
type ClientTransport struct { MaxOpenConns uint64 MinOpenConns uint64 MaxConnIdleTime time.Duration ReadPreference *readpref.ReadPref ServiceNameURIs map[string][]string // contains filtered or unexported fields }
ClientTransport is a client-side mongodb transport.
func (*ClientTransport) GetMgoClient ¶
GetMgoClient obtains mongodb client, cache dsn=>client, save some initialization steps such as reparsing parameters, generating topology server, etc.
func (*ClientTransport) RoundTrip ¶
func (ct *ClientTransport) RoundTrip(ctx context.Context, _ []byte, callOpts ...transport.RoundTripOption) (rspBytes []byte, err error)
RoundTrip sends and receives mongodb packets, returns the mongodb response and puts it in ctx, there is no need to return rspbuf here.
type ClientTransportOption ¶
type ClientTransportOption func(opt *ClientTransport)
ClientTransportOption sets client transport parameter.
func WithOptionInterceptor ¶
func WithOptionInterceptor(f func(dsn string, opts *options.ClientOptions)) ClientTransportOption
WithOptionInterceptor returns an ClientTransportOption which sets mongo client option interceptor
type Config ¶
type Config struct { MinOpen uint64 `yaml:"min_open"` // Minimum number of simultaneous online connections MaxOpen uint64 `yaml:"max_open"` // The maximum number of simultaneous online connections MaxIdleTime time.Duration `yaml:"max_idle_time"` // Maximum idle time per link ReadPreference string `yaml:"read_preference"` // reference on read }
Config mongo is a proxy configuration structure declaration.
type IndexViewer ¶
type IndexViewer interface { // CreateMany creates the interface definition of the index. CreateMany(ctx context.Context, database string, coll string, models []mongo.IndexModel, opts ...*options.CreateIndexesOptions) ([]string, error) CreateOne(ctx context.Context, database string, coll string, model mongo.IndexModel, opts ...*options.CreateIndexesOptions) (string, error) DropOne(ctx context.Context, database string, coll string, name string, opts ...*options.DropIndexesOptions) (bson.Raw, error) DropAll(ctx context.Context, database string, coll string, opts ...*options.DropIndexesOptions) (bson.Raw, error) }
IndexViewer is the interface definition of the index. Refer to the naming of the community open source library, define the index interface separately, and divide the interface according to the function.
type Request ¶
type Request struct { Command string Database string Collection string Arguments map[string]interface{} DriverProxy bool //driver transparent transmission Filter interface{} //driver filter CommArg interface{} //general parameters Opts interface{} //option parameter }
Request mongodb request body
type Response ¶
type Response struct { Result interface{} // contains filtered or unexported fields }
Response mongodb response body
type TxFunc ¶
type TxFunc func(sc mongo.SessionContext) error
TxFunc mongo is a transaction logic function, if an error is returned, it will be rolled back.
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
Package mockmongodb is a generated GoMock package.
|
Package mockmongodb is a generated GoMock package. |