Categorygithub.com/storage-lock/go-mongodb-storage
modulepackage
0.0.0-20230910152351-fd4213588fd5
Repository: https://github.com/storage-lock/go-mongodb-storage.git
Documentation: pkg.go.dev

# README

Mongodb Storage

一、这是什么

以MongoDB为存储引擎的Storage实现,当前仓库为比较底层的存储层实现,你可以与storage-lock结合使用。

二、安装

go get -u github.com/storage-lock/go-mongodb-storage

三、API示例

3.1 从URI创建MongodbStorage

package main

import (
	"context"
	"fmt"
	mongodb_storage "github.com/storage-lock/go-mongodb-storage"
)

func main() {

	// 使用一个uri形式的数据库连接字符串创建ConnectionManager
	uri := "mongodb://root:[email protected]:27017/?connectTimeoutMS=300000"
	connectionManager := mongodb_storage.NewMongoConnectionManager(uri)

	// 然后从这个ConnectionManager创建MongodbStorage
	options := mongodb_storage.NewMongoStorageOptions().SetConnectionManager(connectionManager)
	storage, err := mongodb_storage.NewMongoStorage(context.Background(), options)
	if err != nil {
		panic(err)
	}
	fmt.Println(storage.GetName())

}

3.2 从mongo.Client创建MongodbStorage

package main

import (
	"context"
	"fmt"
	mongodb_storage "github.com/storage-lock/go-mongodb-storage"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

func main() {

	// 假设已经在其它地方初始化数据库连接得到了一个*mongo.Client
	uri := "mongodb://root:[email protected]:27017/?connectTimeoutMS=300000"
	client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(uri))
	if err != nil {
		panic(err)
	}

	// 则可以从这个*mongo.Client中创建一个Mongodb Storage
	connectionManager := mongodb_storage.NewMongoConnectionManagerFromClient(client)
	options := mongodb_storage.NewMongoStorageOptions().SetConnectionManager(connectionManager)
	storage, err := mongodb_storage.NewMongoStorage(context.Background(), options)
	if err != nil {
		panic(err)
	}
	fmt.Println(storage.GetName())

}

# Packages

No description provided by the author

# Functions

No description provided by the author
NewMongoConnectionManagerFromClient 复用已经存在的mongo client,从其创建连接管理器.
NewMongoConnectionManagerFromURI 从Mongo uri创建连接管理器.
NewMongoStorage 创建一个基于MongoDB的存储引擎.
No description provided by the author
No description provided by the author

# Constants

No description provided by the author
No description provided by the author

# Variables

ErrCollectionNameEmpty 参数中集合名字为空.
ErrConnectionManagerNil 连接管理器没有指定.
ErrDatabaseNameEmpty 参数中数据库名字为空.

# Structs

ListMongoLockIterator 用于迭代列出mongo中的所有的锁.
MongoConnectionManager 负责维护与Mongo数据库的连接.
MongoLock 锁在Mongo中存储的结构.
MongoStorage MongoDB的存储引擎实现.
MongoStorageOptions Mongo的存储选项.