mongo的or、sort、limit

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
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容