从 Go Mongo API 返回单个对象

发布于 2025-01-13 20:31:59 字数 2253 浏览 0 评论 0原文

我遇到的问题是,下面的代码仅在令牌不在数组中时才有效(或者被认为是其余令牌的原始刷新令牌)。我浪费了太多宝贵的精力试图找到一种方法来返回正确的会话。

// Find the current sessions info
currentSession := model.Session{}
lookupSession := bson.D{{Key: "token", Value: refreshToken}}
_ := tokensCol.FindOne(ctx, lookupSession).Decode(&currentSession)
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NDcyMzAxNTJ9.R-Tm8sgs..."
userID: "1"
userAgent: ""
ip: ""
exp: 1647230152
valid: false
original: true
family:
   0:
      token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NDcyMzAyNTJ9.noqkeUYW..."
      userID: "1"
      userAgent: ""
      ip: ""
      exp: 1647230252
      valid: true
      original: false

是否有一站式服务来返回令牌驻留在文档顶层或嵌套在 family 数组中的对象?下面的代码部分有效,但返回以原始标记开始的整个文档。不确定如何调整检索到的数据

currentSession := model.Session{}
filter := bson.M{
    "family": bson.M{
        "$elemMatch": bson.M{"token": refreshToken},
    },
}
_ = tokensCol.FindOne(ctx, filter).Decode(&currentSession)

fmt.Println(currentSession)

返回:

{c8ncjdiaas68dh9fq1d0 eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NDcyMzcwNjJ9.KlR1mdC0UBnGfxr31MZwzoE7tTVQwuN5uciteSqh8Kg 1 1647237062 false true [{c8ncjhaaas68dh9fq1dg eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NDcyMzcwNzd9.lx6MIBN_pzlKei9DWr0-k-kvr6yLJ4XfhGSTNNVqRKY 1 1647237077 true false []}]}

编辑:很好,但是..我仍然只返回文档顶部的原始标记,而不是我需要的嵌套对象?唯一的区别是,当我说 "token":refreshToken 而不是抛出 "token": 1 时,令牌的值会正确返回,

session := model.Session{}
lookupSession := bson.M{
    "$or": []bson.M{
        {"token": refreshToken},
        {"family.token": refreshToken},
    },
}
opts := options.FindOne().SetProjection(
    bson.M{
        "token":     refreshToken,
        "userid":    1,
        "useragent": 1,
        "ip":        1,
        "exp":       1,
        "valid":     1,
        "original":  1,
    },
)
lookupErr := tokensCol.FindOne(ctx, lookupSession, opts).Decode(&session)

我确实需要从返回的文档FindOne 包含对象中的特定数据,而不是文档的顶层数据。因为我不会对此进行检查,因为它是无效的。对此有何评论或建议?

The trouble I've been having is that the code bellow only works if the token isn't in an array, (or is considered the original refresh token the rest descend from). I'm wasting so much precious energy trying to find a way to return the correct session.

// Find the current sessions info
currentSession := model.Session{}
lookupSession := bson.D{{Key: "token", Value: refreshToken}}
_ := tokensCol.FindOne(ctx, lookupSession).Decode(¤tSession)
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NDcyMzAxNTJ9.R-Tm8sgs..."
userID: "1"
userAgent: ""
ip: ""
exp: 1647230152
valid: false
original: true
family:
   0:
      token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NDcyMzAyNTJ9.noqkeUYW..."
      userID: "1"
      userAgent: ""
      ip: ""
      exp: 1647230252
      valid: true
      original: false

Is there a one stop shop for returning the object the token resides either at the top-level of the document or nested within the family array? The code bellow partially works, but returns the entire document starting with the original token. Not sure how to shape the data retrieved

currentSession := model.Session{}
filter := bson.M{
    "family": bson.M{
        "$elemMatch": bson.M{"token": refreshToken},
    },
}
_ = tokensCol.FindOne(ctx, filter).Decode(¤tSession)

fmt.Println(currentSession)

Returns:

{c8ncjdiaas68dh9fq1d0 eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NDcyMzcwNjJ9.KlR1mdC0UBnGfxr31MZwzoE7tTVQwuN5uciteSqh8Kg 1 1647237062 false true [{c8ncjhaaas68dh9fq1dg eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NDcyMzcwNzd9.lx6MIBN_pzlKei9DWr0-k-kvr6yLJ4XfhGSTNNVqRKY 1 1647237077 true false []}]}

EDIT: Great, however.. I'm still only returning the original token at the top of the document and not the nested object that I need? The only difference is that the value of the token returns correctly when instead of throwing "token": 1, I say "token": refreshToken

session := model.Session{}
lookupSession := bson.M{
    "$or": []bson.M{
        {"token": refreshToken},
        {"family.token": refreshToken},
    },
}
opts := options.FindOne().SetProjection(
    bson.M{
        "token":     refreshToken,
        "userid":    1,
        "useragent": 1,
        "ip":        1,
        "exp":       1,
        "valid":     1,
        "original":  1,
    },
)
lookupErr := tokensCol.FindOne(ctx, lookupSession, opts).Decode(&session)

I really need the returned document from FindOne to include specific data from the object and not the top-level of the document. As I won't be running checks on that, as it would be invalid. Any Comments or advice for this??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不是你的备胎 2025-01-20 20:31:59

因此,基本上您想要找到一个具有 token 字段的文档,或者 family 数组中具有一个具有给定值的 token 字段的元素?这只是一个 $or 条件,因此请使用此 lookupSession 过滤文档:

lookupSession := bson.M{
    "$or": []bson.M{
        {"token": refreshToken},
        {"family.token": refreshToken},
    },
}

如果您不想检索完整的结果文档,请使用投影。有关示例,请参阅 如何使用官方 mongo-go-driver 过滤 mongo 文档中的字段

So basically you want to find a document that has a token field OR an element in the family array that has a token field with a given value? That's simply an $or condition, so use this lookupSession filter document:

lookupSession := bson.M{
    "$or": []bson.M{
        {"token": refreshToken},
        {"family.token": refreshToken},
    },
}

If you don't want to retrieve the complete result document, use projection. For an example, see How to filter fields from a mongo document with the official mongo-go-driver

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文