英文:
BiDirectional Key to <-> "CompositKey" lookup in GAE?
问题
我正在GAE上编写一个Go应用程序
我有一个名为Connection{Token, ToAdress, FromAdress}
的类型
我想将它存储在数据存储中,以便我可以通过GetConnectionByT(Token string)
进行查找
但也可以通过GetConnectionByA(FromAdress,ToAdress string)
进行查找
怎样做才是最好的方式?目前,我的Connect类型有两个不同的.key()
函数,并且每次添加时都会插入具有不同键的重复条目,但这感觉非常错误
datastore.NewQuery("Connection").Filter
是正确的方法吗?似乎键应该更快更便宜?但如果不是,如何过滤多个字段?
英文:
Am writing a Go application on GAE
And I have a type called Connection{Token, ToAdress, FromAdress}
I want to store it in the datastore so that I can look it up via
GetConnectionByT(Token string)
But also via
GetConnectionByA(FromAdress,ToAdress string)
What is the best way of doing this? Right now my Connect type have two different .key()
functions and I insert duplicate entry's with different keys every time I add but this feels really really wrong
Is datastore.NewQuery("Connection").Filter
the way to go? Seames that a key should be faster and cheaper? But if not how do I filter multiple fields?
答案1
得分: 1
如果您查看数据存储查询过滤器的文档,您会注意到多个过滤器是AND连接的。所以您可以这样做:
q := datastore.NewQuery("Connection").
Filter("FromAddress =", a1).
Filter("ToAddress =", a2).
Order("-FromAddress")
如果您对实体有一个键,那么使用键可能会更快。然而,从您问题的描述中很难确定。
英文:
If you check the documentation for datastore.Query.Filter you'll note that multiple filters are ANDed together. So you can do this kind of thing:
q := datastore.NewQuery("Connection").
Filter("FromAddress =", a1).
Filter("ToAddress =", a2).
Order("-FromAddress")
If you have a key for the entity in question then yes, using the key will probably be faster. However it's a bit difficult to tell from the description in your question.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论