mongodb

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2024 License: BSD-3-Clause Imports: 7 Imported by: 0

README

codecov GoDoc

This package wraps the go mongodb driver by providing a so-called "Connector", this makes the mongodb connection testable/mockable. The original driver is not really testable, it is hard/impossible to mock the package.

Usually in go the interfaces are defined at the consumer side, but in this case an interface is provided to keep the codebase small.

The provided connector interface can easily be mocked using mockery.

Additionaly this package provides some datatypes, like UUID, ObjectId, NullString, nullable numbers and a datatype for storing binary data.

Connector

Constructing a new connector, by returning a StdConnector:

connector, err := mongodb.NewConnector(mongodb.NewParams{
    Uri:      "mongodb://user:pass@127.0.0.1/mydb",
    Database: "mydb",
})

Setting the target collection, all operations are executed against this collection, a copy of the connector is returned leaving the original connector unmodified:

connector = connector.WithCollection("Users")

Setting the context, by default context.TODO() is used, a new connector will be returned using the supplied context for consecutive calls:

connector = connector.WithContext(context.Background())

Finding a row:

var ret User

err := connector.FindOne(bson.D{{"_id", id}}).Decode(&ret)

The functions are exactly the same as those of the mongodb driver, e.g. Find, FindOne, Count, UpdateOne, ...

Sequences

Besided the wrapped functions of the mongodb driver, a function for fetching sequence numbers was implemented, it returns the next number by using FindOneAndUpdate(), with the upsert option.

nextNumber, err := connector.GetNextSeq("Users")

The sequence numbers are stored into a "Sequences" collection, the _id is the provided name ("Users" in this case) and the current number is stored into the "Current" field. If no name was provided, the name of the current collection is used. You can optionally provide the name of the collection where the sequences are stored.

Datatypes

Besides the BSON conversation, all datatypes are supporting JSON encoding/decoding, by implementing the marshal/unmarshal functions.

ObjectId

The types package adds an ObjectId replacement of the mongodb drivers primitive.ObjectId. The original ObjectId has two disadvantages:

  • an empty ObjectId is stored as "000000000000000000000000", instead of null, which is kind of weird.
  • every conversion between a string and an ObjectId has to be done using ObjectIDFromHex(), which adds a lot of extra code.

The ObjectId provided by the types package derives from string, so conversions can be easily done using a simple type cast, but you have to make sure, that the string contains a valid ObjectId in HEX format, otherwise you will get an error, when marshalling it to BSON.

hexOid := "61791c74138d41367e52d832"

objectId := types.ObjectId(hexOid)

UUID

The UUID derives from string for easy conversion, it's BSON represenation is primitive.Binary with the subtype of bson.TypeBinaryUUID. This means it is store as native UUID into the database. An empty UUID is treated as null when converting to BSON.

Under the hood, it uses github.com/google/uuid for parsing/generating values. Invalid values will produce an error if converting to BSON.

uuid := types.NewUuid()

uuidStr := "9f53f39d-62b6-43ac-a267-f25848739aeb"
uuid = types.UUID(uuidStr)

Binary

The binary datatype stores any arbitrary value as binary, the binary subtype is bson.TypeBinaryGeneric. The JSON representation of the binary is base64. It is very useful if you do not want to/can use GridFS, but keep in mind that the maximum BSON document size is 16MBytes.

NullString

The NullString datatype BSON-encodes empty strings to null and vice versa.

NullNumbers

The various number datatypes are treated as BSON-null if their value is 0 oder 0.0 and vice versa.

Example

This is a real life example, demonstrating how the connector could be integrated. It defines the database access layer (UserDb) and connects this to a model (UserModel) by using an interface (UserDbInterface), defined at the consumer side. This makes the codebase easily unit testable.

It also defines the provider functions, as you would use it with wire.

Definitions

package user

import (
    "errors"
    "github.com/mbretter/go-mongodb"
    "github.com/mbretter/go-mongodb/types"
    "github.com/stretchr/testify/assert"
    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/mongo"
    "log"
    "os"
    "testing"
)

// User represents a user entity with an ID, username, and personal details such as firstname and lastname.
type User struct {
    Id        types.ObjectId `bson:"_id"`
    Username  string         `bson:"username,omitempty"`
    Firstname string         `bson:"firstname,omitempty"`
    Lastname  string         `bson:"lastname,omitempty"`
}

// UserDbInterface defines the methods required to interact with the User entity in the database.
// Insert adds a new User to the database and returns an error if the operation fails.
// Update modifies an existing User in the database and returns an UpdateResult and an error if the operation fails.
// Read retrieves a User by their ObjectId and returns the User and an error if the operation fails.
// Delete removes a User by their ObjectId and returns a DeleteResult and an error if the operation fails.
type UserDbInterface interface {
    Insert(*User) error
    Update(user *User) (*mongo.UpdateResult, error)
    Read(id types.ObjectId) (*User, error)
    Delete(id types.ObjectId) (*mongo.DeleteResult, error)
}

// UserModel provides methods for managing user data by integrating with the UserDbInterface.
type UserModel struct {
    db UserDbInterface
}

// ProvideModel initializes a User instance with the provided UserDbInterface.
func ProvideModel(db UserDbInterface) UserModel {
    return UserModel{db: db}
}

func (m UserModel) Create(user User) (User, error) {
    user.Id = types.NewObjectId()
    
    err := m.db.Insert(&user)
    if err != nil {
        return user, err
    }
    
    return user, nil
}

func (m UserModel) ReadById(id types.ObjectId) (*User, error) {
    return m.db.Read(id)
}

func (m UserModel) Update(user User) error {
    _, err := m.db.Update(&user)
    return err
}

func (m UserModel) DeleteById(id types.ObjectId) error {
    _, err := m.db.Delete(id)
    
    return err
}

// UserDb provides methods to interact with the user collection in the database using a Connector interface.
type UserDb struct {
    conn mongodb.Connector
}

func (d *UserDb) Insert(user *User) error {
    _, err := d.conn.InsertOne(user)
    if err != nil {
        return err
    }
    
    return nil
}

func (d *UserDb) Update(user *User) (*mongo.UpdateResult, error) {
    res, err := d.conn.UpdateById(user.Id, bson.D{{"$set", user}})
    
    return res, err
}

func (d *UserDb) Read(id types.ObjectId) (*User, error) {
    var ret User
    
    err := d.conn.FindOne(bson.D{{"_id", id}}).Decode(&ret)
    if err != nil {
        if errors.Is(err, mongo.ErrNoDocuments) {
            return nil, nil
        }
        return nil, err
    }
    
    return &ret, nil
}

func (d *UserDb) Delete(id types.ObjectId) (*mongo.DeleteResult, error) {
    return d.conn.DeleteOne(bson.D{{"_id", id}})
}

func ProviderUserDb(conn mongodb.Connector) *UserDb {
    return &UserDb{
        conn: conn.WithCollection("user"),
    }
}

Main

This is a code fragment wich shows how to put all the components together.

  • build the connector and connect to the database
  • build the database layer (UserDb)
  • build the model (UserModel)
  • do some CRUD operations
func main() {
    conn, err := mongodb.NewConnector(mongodb.NewParams{
        Uri:      os.Getenv("MONGODB_URI"),
        Database: os.Getenv("MONGODB_DB"),
    })
    
    if err != nil {
        log.Fatalf("failed to connect to db: %v\n", err)
    }
    
    userDb := ProviderUserDb(conn)
    userModel := ProvideModel(userDb)
    user := User{
        Username:  "foo@bar.com",
        Firstname: "John",
        Lastname:  "Doe",
    }
    
    user, err = userModel.Create(user)
    if err != nil {
        log.Fatalf("failed to create user: %v", err)
    }
    
    log.Printf("created user: %v", user)
    
    existingUser, err := userModel.ReadById(user.Id)
    if err != nil {
        log.Fatalf("failed to read user: %v", err)
    }
    
    if existingUser == nil {
        log.Fatalf("user not found")
    }
    
    updateUser := User{
        Id:        existingUser.Id,
        Firstname: "Jane",
    }
    
    err = userModel.Update(updateUser)
    if err != nil {
        log.Fatalf("failed to update user: %v", err)
    }
    
    err = userModel.DeleteById(user.Id)
    if err != nil {
        log.Fatalf("failed to delete user: %v", err)
    }
}

Tests

This integration test uses a mock of the Connector, the mock can easily be auto-generated by mockery.

If you would like to unit test the UserModel, you have to mock the UserDbInterface.

func TestCreate(t *testing.T) {
    newUserId := "66cc9ca8c042f7a732b7fc2a"
    types.SetObjectIdGenerator(func() string { return newUserId })
    
    user := User{
        Id:        types.ObjectId(newUserId),
        Username:  "foo@bar.com",
        Firstname: "John",
        Lastname:  "Doe",
    }
    
    tests := []struct {
        name string
        err  error
    }{
        {
            "Success",
            nil,
        },
        {
            "Failed",
            errors.New("some database error occurred"),
        },
    }
    
    for _, test := range tests {
        t.Run(test.name, func(t *testing.T) {
            conn := NewConnectorMock(t)
            conn.EXPECT().WithCollection("user").Return(conn)
    
            userDb := ProviderUserDb(conn)
            userModel := ProvideModel(userDb)
    
            res := mongo.InsertOneResult{}
            conn.EXPECT().InsertOne(&user).Return(&res, test.err)
    
            user, err := userModel.Create(user)
            if test.err == nil {
                assert.Nil(t, err)
                assert.Equal(t, user.Id, types.ObjectId(newUserId))
                assert.Equal(t, user.Username, "foo@bar.com")
                assert.Equal(t, user.Firstname, "John")
                assert.Equal(t, user.Lastname, "Doe")
            } else {
                assert.NotNil(t, err)
            }
        })
    }
}

Documentation

Overview

Package mongodb wraps the go mongodb driver by providing a so-called "Connector", this makes the mongodb connection testable/mockable. The original driver is not really testable, it is hard/impossible to mock the package. Usually in go the interfaces are defined at the consumer side, but in this case an interface is provided to keep the codebase small.

The provided connector interface can easily be mocked using mockery.

Additionaly this package provides some datatypes, like UUID, ObjectId, NullString, nullable numbers and a datatype for storing binary data.

Example

Example demonstrates the process of creating, reading, updating, and deleting a user in a MongoDB database.

package main

import (
	"errors"
	"github.com/mbretter/go-mongodb"
	"github.com/mbretter/go-mongodb/types"
	"github.com/stretchr/testify/assert"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"log"
	"os"
	"testing"
)

// User represents a user entity with an ID, username, and personal details such as firstname and lastname.
type User struct {
	Id        types.ObjectId `bson:"_id"`
	Username  string         `bson:"username,omitempty"`
	Firstname string         `bson:"firstname,omitempty"`
	Lastname  string         `bson:"lastname,omitempty"`
}

// UserDbInterface defines the methods required to interact with the User entity in the database.
// Insert adds a new User to the database and returns an error if the operation fails.
// Update modifies an existing User in the database and returns an UpdateResult and an error if the operation fails.
// Read retrieves a User by their ObjectId and returns the User and an error if the operation fails.
// Delete removes a User by their ObjectId and returns a DeleteResult and an error if the operation fails.
type UserDbInterface interface {
	Insert(*User) error
	Update(user *User) (*mongo.UpdateResult, error)
	Read(id types.ObjectId) (*User, error)
	Delete(id types.ObjectId) (*mongo.DeleteResult, error)
}

// UserModel provides methods for managing user data by integrating with the UserDbInterface.
type UserModel struct {
	db UserDbInterface
}

// ProvideModel initializes a User instance with the provided UserDbInterface.
func ProvideModel(db UserDbInterface) UserModel {
	return UserModel{db: db}
}

func (m UserModel) Create(user User) (User, error) {
	user.Id = types.NewObjectId()

	err := m.db.Insert(&user)
	if err != nil {
		return user, err
	}

	return user, nil
}

func (m UserModel) ReadById(id types.ObjectId) (*User, error) {
	return m.db.Read(id)
}

func (m UserModel) Update(user User) error {
	_, err := m.db.Update(&user)
	return err
}

func (m UserModel) DeleteById(id types.ObjectId) error {
	_, err := m.db.Delete(id)

	return err
}

// UserDb provides methods to interact with the user collection in the database using a Connector interface.
type UserDb struct {
	conn mongodb.Connector
}

func (d *UserDb) Insert(user *User) error {
	_, err := d.conn.InsertOne(user)
	if err != nil {
		return err
	}

	return nil
}

func (d *UserDb) Update(user *User) (*mongo.UpdateResult, error) {

	res, err := d.conn.UpdateById(user.Id, bson.D{{"$set", user}})

	return res, err
}

func (d *UserDb) Read(id types.ObjectId) (*User, error) {
	var ret User

	err := d.conn.FindOne(bson.D{{"_id", id}}).Decode(&ret)
	if err != nil {
		if errors.Is(err, mongo.ErrNoDocuments) {
			return nil, nil
		}
		return nil, err
	}

	return &ret, nil
}

func (d *UserDb) Delete(id types.ObjectId) (*mongo.DeleteResult, error) {
	return d.conn.DeleteOne(bson.D{{"_id", id}})
}

func ProviderUserDb(conn mongodb.Connector) *UserDb {
	return &UserDb{
		conn: conn.WithCollection("user"),
	}
}

// Example demonstrates the process of creating, reading, updating, and deleting a user in a MongoDB database.
func main() {
	conn, err := mongodb.NewConnector(mongodb.NewParams{
		Uri:      os.Getenv("MONGODB_URI"),
		Database: os.Getenv("MONGODB_DB"),
	})

	if err != nil {
		log.Fatalf("failed to connect to db: %v\n", err)
	}

	userDb := ProviderUserDb(conn)
	userModel := ProvideModel(userDb)
	user := User{
		Username:  "foo@bar.com",
		Firstname: "John",
		Lastname:  "Doe",
	}

	user, err = userModel.Create(user)
	if err != nil {
		log.Fatalf("failed to create user: %v", err)
	}

	log.Printf("created user: %v", user)

	existingUser, err := userModel.ReadById(user.Id)
	if err != nil {
		log.Fatalf("failed to read user: %v", err)
	}

	if existingUser == nil {
		log.Fatalf("user not found")
	}

	updateUser := User{
		Id:        existingUser.Id,
		Firstname: "Jane",
	}

	err = userModel.Update(updateUser)
	if err != nil {
		log.Fatalf("failed to update user: %v", err)
	}

	err = userModel.DeleteById(user.Id)
	if err != nil {
		log.Fatalf("failed to delete user: %v", err)
	}
}

// Example_Test tests the scenario for user creation, ensuring correct insertion and data consistency.
// The Connector is mocked using the auto-generated mock by mockery.
// The ObjectId generator function is stubbed, to get reproducable results.
func main(t *testing.T) {
	newUserId := "66cc9ca8c042f7a732b7fc2a"
	types.SetObjectIdGenerator(func() string { return newUserId })

	user := User{
		Id:        types.ObjectId(newUserId),
		Username:  "foo@bar.com",
		Firstname: "John",
		Lastname:  "Doe",
	}

	tests := []struct {
		name string
		err  error
	}{
		{
			"Success",
			nil,
		},
		{
			"Failed",
			errors.New("some database error occurred"),
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			conn := NewConnectorMock(t)
			conn.EXPECT().WithCollection("user").Return(conn)

			userDb := ProviderUserDb(conn)
			userModel := ProvideModel(userDb)

			res := mongo.InsertOneResult{}
			conn.EXPECT().InsertOne(&user).Return(&res, test.err)

			user, err := userModel.Create(user)
			if test.err == nil {
				assert.Nil(t, err)
				assert.Equal(t, user.Id, types.ObjectId(newUserId))
				assert.Equal(t, user.Username, "foo@bar.com")
				assert.Equal(t, user.Firstname, "John")
				assert.Equal(t, user.Lastname, "Doe")
			} else {
				assert.NotNil(t, err)
			}
		})
	}

}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connector

type Connector interface {
	Database() *mongo.Database
	Collection(coll string, opts ...*options.CollectionOptions) *mongo.Collection
	NewGridfsBucket() (*gridfs.Bucket, error)
	WithContext(context.Context) Connector
	WithCollection(coll string, opts ...*options.CollectionOptions) Connector
	Find(filter interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)
	FindOne(filter interface{}, opts ...*options.FindOneOptions) *mongo.SingleResult
	FetchAll(cur *mongo.Cursor, results interface{}) error
	Decode(cur *mongo.Cursor, val interface{}) error
	Next(cur *mongo.Cursor) bool
	Count(filter interface{}, opts ...*options.CountOptions) (cnt int64, err error)
	Distinct(fieldName string, filter interface{}, opts ...*options.DistinctOptions) (res []interface{}, err error)
	FindOneAndDelete(filter interface{}, opts ...*options.FindOneAndDeleteOptions) *mongo.SingleResult
	FindOneAndReplace(filter interface{}, replacement interface{}, opts ...*options.FindOneAndReplaceOptions) *mongo.SingleResult
	FindOneAndUpdate(filter interface{}, update interface{}, opts ...*options.FindOneAndUpdateOptions) *mongo.SingleResult
	UpdateOne(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (res *mongo.UpdateResult, err error)
	UpdateMany(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (res *mongo.UpdateResult, err error)
	UpdateById(id interface{}, update interface{}, opts ...*options.UpdateOptions) (res *mongo.UpdateResult, err error)
	ReplaceOne(filter interface{}, update interface{}, opts ...*options.ReplaceOptions) (res *mongo.UpdateResult, err error)
	InsertOne(document interface{}, opts ...*options.InsertOneOptions) (res *mongo.InsertOneResult, err error)
	InsertMany(document []interface{}, opts ...*options.InsertManyOptions) (res *mongo.InsertManyResult, err error)
	DeleteOne(filter interface{}, opts ...*options.DeleteOptions) (res *mongo.DeleteResult, err error)
	DeleteMany(filter interface{}, opts ...*options.DeleteOptions) (res *mongo.DeleteResult, err error)
	Aggregate(pipeline interface{}, opts ...*options.AggregateOptions) (cur *mongo.Cursor, err error)
	Drop() error
	Watch(pipeline interface{}, opts ...*options.ChangeStreamOptions) (stream *mongo.ChangeStream, err error)
	GetNextSeq(name string, opts ...string) (res int64, err error)
}

Connector provides methods for database and collection operations.

type NewParams

type NewParams struct {
	Uri      string
	Database string
}

NewParams holds the parameters required to establish a new connection to a database.

type StdConnector

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

StdConnector handles connections and interactions with the MongoDB client, database, and collections.

func NewConnector

func NewConnector(params NewParams) (*StdConnector, error)

NewConnector establishes a new connection to the mongo database using the provided parameters. It returns a StdConnector

func (*StdConnector) Aggregate

func (conn *StdConnector) Aggregate(pipeline interface{}, opts ...*options.AggregateOptions) (cur *mongo.Cursor, err error)

Aggregate executes an aggregation framework pipeline on the collection. The 'pipeline' parameter specifies an array of aggregation stages. The 'opts' parameters specify optional settings for the aggregate operation. It returns a cursor that iterates over the resulting documents.

func (*StdConnector) Collection

func (conn *StdConnector) Collection(coll string, opts ...*options.CollectionOptions) *mongo.Collection

Collection returns a mongo.Collection object for the specified collection name with additional options if provided.

func (*StdConnector) Count

func (conn *StdConnector) Count(filter interface{}, opts ...*options.CountOptions) (cnt int64, err error)

Count returns the count of documents matching the given filter and options or an error if the collection is not set.

func (*StdConnector) Database

func (conn *StdConnector) Database() *mongo.Database

Database returns the current mongo.Database instance associated with the StdConnector.

func (*StdConnector) Decode

func (conn *StdConnector) Decode(cur *mongo.Cursor, val interface{}) error

Decode decodes the current document pointed to by the cursor into the provided value. Returns an error if decoding fails.

func (*StdConnector) DeleteMany

func (conn *StdConnector) DeleteMany(filter interface{}, opts ...*options.DeleteOptions) (res *mongo.DeleteResult, err error)

DeleteMany deletes multiple documents from the collection that match the provided filter. Parameters: - filter: A document describing the documents to be deleted. - opts: Optional. Additional options to apply to the delete operation. Returns: - res: A DeleteResult containing the outcome of the delete operation. - err: An error if the operation fails.

func (*StdConnector) DeleteOne

func (conn *StdConnector) DeleteOne(filter interface{}, opts ...*options.DeleteOptions) (res *mongo.DeleteResult, err error)

DeleteOne deletes a single document from the collection that matches the provided filter.

func (*StdConnector) Distinct

func (conn *StdConnector) Distinct(fieldName string, filter interface{}, opts ...*options.DistinctOptions) (res []interface{}, err error)

Distinct retrieves distinct values for a specified field in the collection that matches the given filter and options. Returns a slice of distinct values or an error if the collection is not set or the operation fails.

func (*StdConnector) Drop

func (conn *StdConnector) Drop() (err error)

Drop removes the current collection from the database and returns an error if unsuccessful.

func (*StdConnector) FetchAll

func (conn *StdConnector) FetchAll(cur *mongo.Cursor, results interface{}) (err error)

FetchAll retrieves all the documents from the provided MongoDB cursor and stores them in the results interface.

func (*StdConnector) Find

func (conn *StdConnector) Find(filter interface{}, opts ...*options.FindOptions) (cur *mongo.Cursor, err error)

Find executes a find query in the collection with the given filter and options. Returns a cursor to the found documents or an error if the collection is not set or if the query fails.

func (*StdConnector) FindOne

func (conn *StdConnector) FindOne(filter interface{}, opts ...*options.FindOneOptions) (res *mongo.SingleResult)

FindOne retrieves a single document from the collection based on the provided filter and options. Returns a SingleResult containing the document or an error if the collection is not set.

func (*StdConnector) FindOneAndDelete

func (conn *StdConnector) FindOneAndDelete(filter interface{}, opts ...*options.FindOneAndDeleteOptions) (cur *mongo.SingleResult)

FindOneAndDelete deletes a single document from the collection that matches the provided filter and returns the deleted document.

func (*StdConnector) FindOneAndReplace

func (conn *StdConnector) FindOneAndReplace(filter interface{}, replacement interface{}, opts ...*options.FindOneAndReplaceOptions) (cur *mongo.SingleResult)

FindOneAndReplace replaces a single document in the collection matching the given filter with the provided replacement.

func (*StdConnector) FindOneAndUpdate

func (conn *StdConnector) FindOneAndUpdate(filter interface{}, update interface{}, opts ...*options.FindOneAndUpdateOptions) (cur *mongo.SingleResult)

FindOneAndUpdate updates a single document matching the filter and returns the updated document.

func (*StdConnector) GetNextSeq

func (conn *StdConnector) GetNextSeq(name string, opts ...string) (seq int64, err error)

GetNextSeq increments and retrieves the next sequence number for a given name within the specified collection.

func (*StdConnector) InsertMany

func (conn *StdConnector) InsertMany(document []interface{}, opts ...*options.InsertManyOptions) (res *mongo.InsertManyResult, err error)

InsertMany inserts multiple documents into the collection. It returns an InsertManyResult containing the IDs of the inserted documents or an error if the insertion fails.

func (*StdConnector) InsertOne

func (conn *StdConnector) InsertOne(document interface{}, opts ...*options.InsertOneOptions) (res *mongo.InsertOneResult, err error)

InsertOne inserts a single document into the collection. It returns the result of the insertion and any error encountered. The method takes a document to be inserted and optional insertion options.

func (*StdConnector) NewGridfsBucket

func (conn *StdConnector) NewGridfsBucket() (*gridfs.Bucket, error)

NewGridfsBucket creates a new GridFS bucket for the current database.

func (*StdConnector) Next

func (conn *StdConnector) Next(cur *mongo.Cursor) bool

Next progresses the given MongoDB cursor to the next document and returns true if a next document is available.

func (*StdConnector) ReplaceOne

func (conn *StdConnector) ReplaceOne(filter interface{}, update interface{}, opts ...*options.ReplaceOptions) (res *mongo.UpdateResult, err error)

ReplaceOne replaces a single document in the collection that matches the specified filter with the provided update.

func (*StdConnector) UpdateById

func (conn *StdConnector) UpdateById(id interface{}, update interface{}, opts ...*options.UpdateOptions) (res *mongo.UpdateResult, err error)

UpdateById updates a single document in the collection based on its ID.

func (*StdConnector) UpdateMany

func (conn *StdConnector) UpdateMany(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (res *mongo.UpdateResult, err error)

UpdateMany updates multiple documents in the collection based on the provided filter and update parameters. It returns a mongo.UpdateResult containing details about the operation or an error if one occurred.

func (*StdConnector) UpdateOne

func (conn *StdConnector) UpdateOne(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (res *mongo.UpdateResult, err error)

UpdateOne executes an update operation on a single document in the collection. Parameters are a filter to select the document, an update to apply, and optional update options. Returns the result of the update operation or an error if the operation fails.

func (*StdConnector) Watch

func (conn *StdConnector) Watch(pipeline interface{}, opts ...*options.ChangeStreamOptions) (stream *mongo.ChangeStream, err error)

Watch starts a change stream against the collection of the StdConnector, based on the given pipeline and options. It returns a pointer to a mongo.ChangeStream for iterating the changes, or an error if the collection is not set.

func (*StdConnector) WithCollection

func (conn *StdConnector) WithCollection(coll string, opts ...*options.CollectionOptions) Connector

WithCollection returns a copy of StdConnector with the specified collection and optional collection options.

func (*StdConnector) WithContext

func (conn *StdConnector) WithContext(ctx context.Context) Connector

WithContext returns a copy of the StdConnector with the specified context.

Directories

Path Synopsis
Package types provides a binary datatype.
Package types provides a binary datatype.

Jump to

Keyboard shortcuts

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