mongo

package
v3.1.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 7, 2018 License: MIT Imports: 10 Imported by: 19

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client represents a MongoDB client. This wraps the Mongo-Go-Driver client.

func NewClient

func NewClient(config ClientConfig) (*Client, error)

NewClient creates new client based on the ClientConfig provided.

func (*Client) Connect

func (c *Client) Connect() error

Connect connects the created client to Database. This is a no-op if the client is already connected. This is also run by default unless "NoDefaultConnect" is specified in ClientConfig.

func (*Client) Database

func (c *Client) Database(dbName string) *mgo.Database

Database returns a handle for a given database.

func (*Client) Disconnect

func (c *Client) Disconnect() error

Disconnect disconnects the created client from Database. This is a no-op if the client is already disconnected.

func (*Client) DriverClient

func (c *Client) DriverClient() *mgo.Client

DriverClient returns the wrapped Mongo-Go-Driver client.

type ClientConfig

type ClientConfig struct {
	Hosts               []string
	Username            string
	Password            string
	NoDefaultConnect    bool
	TimeoutMilliseconds uint32
}

ClientConfig represents the configuration for a client.

type Collection

type Collection struct {
	Connection *ConnectionConfig
	Database   string
	Name       string
	// Indexes to be created when creating collection
	Indexes      []IndexConfig
	SchemaStruct interface{}
	// contains filtered or unexported fields
}

Collection represents the MongoDB collection.

func EnsureCollection

func EnsureCollection(c *Collection) (*Collection, error)

EnsureCollection creates new collection with the provided indexes. If the collection already exists, it will just return the existing collection.

func (*Collection) Aggregate added in v1.5.0

func (c *Collection) Aggregate(pipeline interface{}) ([]interface{}, error)

Aggregate runs an aggregation framework pipeline See https://docs.mongodb.com/manual/aggregation/.

func (*Collection) Collection

func (c *Collection) Collection() *mgo.Collection

Collection returns the embedded Mongo-Go-Driver Collection. Use this only when absolutely required, and prefer inbuilt functions over functions from this.

func (*Collection) DeleteMany

func (c *Collection) DeleteMany(filter interface{}) (*mgo.DeleteResult, error)

DeleteMany deletes multiple documents from the collection. The filter-data must match the schema provided at the time of Collection- creation. Update the Collection.SchemaStruct if new schema is required.

func (*Collection) Find

func (c *Collection) Find(
	filter interface{},
	opts ...findopt.Find,
) ([]interface{}, error)

Find finds the documents matching the filter. The filter-data must match the schema provided at the time of Collection- creation. Update the Collection.SchemaStruct if new schema is required. A map can also be provided as filter. For example, a find-query in MongoDB such as:

{hits: {$gt: 4, $lt: 9}}

can be represented in a map as:

 map[string]interface{}{
	  "hits": map[string]interface{
     "$gt": 4,
     "$lt": 9,
   },
 }

func (*Collection) FindOne

func (c *Collection) FindOne(
	filter interface{},
	opts ...findopt.One,
) (interface{}, error)

FindOne returns single result that matches the provided filter. The filter-data must match the schema provided at the time of Collection- creation. Update the Collection.SchemaStruct if new schema is required.

func (*Collection) InsertMany

func (c *Collection) InsertMany(
	data []interface{},
) (*[]mgo.InsertOneResult, error)

InsertMany inserts the provided data into Collection. Currently, batching is not implemented for this operation. Because of this, extremely large sets of documents will not fit into a single BSON document to be sent to the server, so the operation will fail. The data must match the schema provided at the time of Collection- creation. Update the Collection.SchemaStruct if new schema is required.

func (*Collection) InsertOne

func (c *Collection) InsertOne(data interface{}) (*mgo.InsertOneResult, error)

InsertOne inserts the provided data into Collection. The data must match the schema provided at the time of Collection- creation. Update the Collection.SchemaStruct if new schema is required.

func (*Collection) UpdateMany

func (c *Collection) UpdateMany(
	filter interface{},
	update interface{},
) (*mgo.UpdateResult, error)

UpdateMany updates multiple documents in the collection. A map or a struct can be supplied as filter-data or update-data, both are transformed into BSON using bson#NewDocumentEncoder#EncodeDocument.

type ConnectionConfig

type ConnectionConfig struct {
	Client  *Client
	Timeout uint32
}

ConnectionConfig defines the Client to use for communicating with MongoDB, and the Timeout for that client.

type IndexColumnConfig

type IndexColumnConfig struct {
	Name        string
	IsDescOrder bool
}

IndexColumnConfig defines configuration for a column in index-definition.

type IndexConfig

type IndexConfig struct {
	ColumnConfig []IndexColumnConfig
	IsUnique     bool
	Name         string
}

IndexConfig defines configuration for indexes to be created when creating this collection.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL