xian9yu 发布的文章
Linux 安装 rust
环境: Ubuntu Desktop 22.04.2 LTS
安装编译依赖:
sudo apt update
sudo apt install -y git gcc clang curl libssl-dev llvm libudev-dev pkg-config file make cmake build-essential
安装 rust命令:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
回车后根据提示安装
卸载 rust环境:
rustup self uninstall
在 Ubuntu 系统上使用 docker && docker-compose
环境: Ubuntu 22.04.3 LTS
软件安装 && 卸载
1 .Install
sudo apt install docker.io docker-compose
测试安装:
$ docker -v
Docker version 20.10.21, build 20.10.21-0ubuntu1~22.04.3
$ docker-compose -v
docker-compose version 1.29.2, build unknown
golang mongo-go-driver 批量查询条件拼接
os: ubuntu 22.04 LTS
golang: v1.20
mongo-go-driver: v1.11.2
// 获取用户传入查询参数
var (
filtersNumber = ctx.URLParam("filters[number]")
filtersName = ctx.URLParam("filters[name]")
filtersShortName = ctx.URLParam("filters[short_name]")
filtersAddress = ctx.URLParam("filters[address]")
filtersContact = ctx.URLParam("filters[contact]")
filtersPhone = ctx.URLParam("filters[phone]")
filtersTypeIds = ctx.URLParam("filters[type_ids]")
filter bson.D
)
// 拼接接收到的参数(用户传入参数)
if filtersNumber != "" {
filter = append(filter, bson.E{Key: "number", Value: bson.D{
{
Key: "$regex", Value: primitive.Regex{Pattern: filtersNumber},
},
}})
}
if filtersName != "" {
filter = append(filter, bson.E{Key: "name", Value: bson.D{
{
Key: "$regex", Value: primitive.Regex{Pattern: filtersName},
},
}})
}
if filtersShortName != "" {
filter = append(filter, bson.E{Key: "short_name", Value: bson.D{
{
Key: "$regex", Value: primitive.Regex{Pattern: filtersShortName},
},
}})
}
if filtersAddress != "" {
filter = append(filter, bson.E{Key: "address", Value: bson.D{
{
Key: "$regex", Value: primitive.Regex{Pattern: filtersAddress},
},
}})
}
if filtersContact != "" {
filter = append(filter, bson.E{Key: "contact", Value: bson.D{
{
Key: "$regex", Value: primitive.Regex{Pattern: filtersContact},
},
}})
}
if filtersPhone != "" {
filter = append(filter, bson.E{Key: "phone", Value: bson.D{
{
Key: "$regex", Value: primitive.Regex{Pattern: filtersPhone},
},
}})
}
if filtersTypeIds != "" {
filter = append(filter, bson.E{Key: "type_ids", Value: bson.D{
{
Key: "$regex", Value: primitive.Regex{Pattern: filtersTypeIds},
},
}})
} else {
filter = append(filter, bson.E{Key: "type_ids", Value: bson.D{{Key: "$in", Value: bson.A{"000000000000000000000009", "000000000000000000000010", "000000000000000000000011", "000000000000000000000012"}}}})
}
// 获取用户传入排序
var (
sortNumber = ctx.URLParam("sort[number]")
sortName = ctx.URLParam("sort[name]")
sortShortName = ctx.URLParam("sort[short_name]")
sortAddress = ctx.URLParam("sort[address]")
sortContact = ctx.URLParam("sort[contact]")
sortPhone = ctx.URLParam("sort[phone]")
sortTypeIds = ctx.URLParam("sort[type_ids]")
sort bson.D
)
// 拼接接收到的排序参数
if sortNumber != "" {
sn, _ := strconv.Atoi(sortNumber)
sort = append(sort, bson.E{Key: "number", Value: sn})
}
if sortName != "" {
sn, _ := strconv.Atoi(sortName)
sort = append(sort, bson.E{Key: "name", Value: sn})
}
if sortShortName != "" {
sn, _ := strconv.Atoi(sortShortName)
sort = append(sort, bson.E{Key: "short_name", Value: sn})
}
if sortAddress != "" {
sn, _ := strconv.Atoi(sortAddress)
sort = append(sort, bson.E{Key: "address", Value: sn})
}
if sortContact != "" {
sn, _ := strconv.Atoi(sortContact)
sort = append(sort, bson.E{Key: "contact", Value: sn})
}
if sortPhone != "" {
sn, _ := strconv.Atoi(sortPhone)
sort = append(sort, bson.E{Key: "phone", Value: sn})
}
if sortTypeIds != "" {
sn, _ := strconv.Atoi(sortAddress)
sort = append(sort, bson.E{Key: "type_ids", Value: sn})
}
// 默认排序
if len(sort) < 1 {
sort = append(sort, bson.E{Key: "created_at", Value: 1})
}
// 查询 collection
collection := mdb.Database("example_db").Collection("example_collection")
// mongo 查询参数
opts := &options.FindOptions{}
opts.SetLimit(int64(size)). // 限制数
SetSkip(int64((page - 1) * size)). // 分页
SetSort(sort) // 排序
logger.Debug.Println(filter)
// mongo sql 执行
find, err := collection.Find(ctxTODO, filter,
opts)
if err != nil {
return err
}
var mp []mongoModels.ExampleCollection
for find.Next(ctxTODO) {
var name mongoModels.Partner
_ = find.Decode(&name)
mp = append(mp, name)
}
Ubuntu安装及配置MySQL8.0
安装环境:
系统: Ubuntu22.04 LTS(64bit)
SQL: MySQL8.0
一、 MySQL安装
1. 更新 apt 索引
sudo apt update
2. 安装数据库, 在安装过程中, 会自动卸载已经安装的老版本数据
sudo apt install mysql-server-8.0
sudo service mysql restart
mysql -u root -p