标签 批量查询 下的文章

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)
}