mongodb

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 8 Imported by: 0

README

MongoDB

概念对比

MongoDB存储结构 RDBMS存储结构
database database
collection table
document row
field column
index 索引
primary key primary key

Docker部署

下载镜像:

docker pull bitnami/mongodb:latest
docker pull bitnami/mongodb-exporter:latest

带密码安装:

docker run -itd \
    --name mongodb-server \
    -p 27017:27017 \
    -e MONGODB_ROOT_USER=root \
    -e MONGODB_ROOT_PASSWORD=123456 \
    -e MONGODB_USERNAME=test \
    -e MONGODB_PASSWORD=123456 \
    -e MONGODB_DATABASE=finances \
    bitnami/mongodb:latest

不带密码安装:

docker run -itd \
    --name mongodb-server \
    -p 27017:27017 \
    -e ALLOW_EMPTY_PASSWORD=yes \
    bitnami/mongodb:latest

有两点需要注意:

  1. 如果需要映射数据卷,需要把本地路径的所有权改到1001:sudo chown -R 1001:1001 data/db,否则会报错: ‘mkdir: cannot create directory ‘/bitnami/mongodb’: Permission denied’
  2. 从MongoDB 5.0开始,有些机器运行会报错:Illegal instruction,这是因为机器硬件不支持 AVX 指令集 的缘故,没办法,MongoDB降级吧。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient added in v0.0.11

func NewClient(logger log.Logger, cfg *conf.Bootstrap, opts ...mongodbCrud.Option) (*mongodbCrud.Client, error)

Types

type Client added in v0.0.11

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

func (*Client) CheckConnect added in v0.0.11

func (c *Client) CheckConnect()

CheckConnect 检查MongoDB连接状态

func (*Client) Close added in v0.0.11

func (c *Client) Close()

Close 关闭MongoDB客户端

func (*Client) DeleteOne added in v0.0.11

func (c *Client) DeleteOne(ctx context.Context, collection string, filter interface{}) (*mongoV2.DeleteResult, error)

DeleteOne 删除单个文档

func (*Client) Find added in v0.0.11

func (c *Client) Find(ctx context.Context, collection string, filter interface{}, results interface{}) error

Find 查询多个文档

func (*Client) FindOne added in v0.0.11

func (c *Client) FindOne(ctx context.Context, collection string, filter interface{}, result interface{}) error

FindOne 查询单个文档

func (*Client) InsertMany added in v0.0.11

func (c *Client) InsertMany(ctx context.Context, collection string, documents []interface{}) (*mongoV2.InsertManyResult, error)

InsertMany 插入多个文档

func (*Client) InsertOne added in v0.0.11

func (c *Client) InsertOne(ctx context.Context, collection string, document interface{}) (*mongoV2.InsertOneResult, error)

InsertOne 插入单个文档

func (*Client) UpdateOne added in v0.0.11

func (c *Client) UpdateOne(ctx context.Context, collection string, filter, update interface{}) (*mongoV2.UpdateResult, error)

UpdateOne 更新单个文档

Jump to

Keyboard shortcuts

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