mongo的or、sort、limit
2023-01-30 本文已影响0人
彳亍口巴
mongo的or、sort、limit
// GetMongoBullet 获取弹幕
func GetMongoBullet(ctx *srfs.Context, uin int64, batchSize int32, lastBulletID string,
timeST int64) ([]MongoBulletInfo, error) {
client, err := getMongoClient(ctx)
if err != nil {
ctx.Error("get mongo client fail err:%v", err)
return nil, fmt.Errorf("call GetClient failed, err: %v", err)
}
defer client.Release(ctx)
var bulletList = make([]MongoBulletInfo, 0, batchSize)
filter := bson.D{
bson.E{Key: "uin", Value: uin},
bson.E{Key: "$or", Value: bson.A{
bson.D{{"timeST", timeST}, {"bulletID", bson.M{"$gt": lastBulletID}}},
bson.D{{"timeST", bson.M{"$lt": timeST}}},
}},
}
option := &options.FindOptions{}
option.SetHint("uin_1_timeST_-1_bulletID_1")
option.SetSort(bson.D{{Key: "uin", Value: 1}, {Key: "timeST", Value: -1}, {Key: "bulletID", Value: 1}})
option.SetLimit(int64(batchSize))
col := client.Database(bulletDBName).Collection(bulletCollectionName)
cur, curErr := col.Find(ctx, filter, option)
if curErr != nil {
ctx.Error("find mongo data err:%+v", curErr)
return bulletList, curErr
}
allErr := cur.All(ctx, &bulletList)
if allErr != nil {
ctx.Error("get all mongo data err:%+v", allErr)
return bulletList, allErr
}
return bulletList, nil
}