从 Go Mongo API 返回单个对象
我遇到的问题是,下面的代码仅在令牌不在数组中时才有效(或者被认为是其余令牌的原始刷新令牌)。我浪费了太多宝贵的精力试图找到一种方法来返回正确的会话。
// 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
是否有一站式服务来返回令牌驻留在文档顶层或嵌套在 family
数组中的对象?下面的代码部分有效,但返回以原始标记开始的整个文档。不确定如何调整检索到的数据
currentSession := model.Session{}
filter := bson.M{
"family": bson.M{
"$elemMatch": bson.M{"token": refreshToken},
},
}
_ = tokensCol.FindOne(ctx, filter).Decode(¤tSession)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,基本上您想要找到一个具有
token
字段的文档,或者family
数组中具有一个具有给定值的token
字段的元素?这只是一个$or
条件,因此请使用此lookupSession
过滤文档:如果您不想检索完整的结果文档,请使用投影。有关示例,请参阅 如何使用官方 mongo-go-driver 过滤 mongo 文档中的字段
So basically you want to find a document that has a
token
field OR an element in thefamily
array that has atoken
field with a given value? That's simply an$or
condition, so use thislookupSession
filter document: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