如何使用redis-om获取数据?

huangapple go评论80阅读模式
英文:

How to fetch data using redis-om?

问题

我想使用redis-om从Redis数据库中获取特定用户的信息。

const album = await albumRepository.fetch('01FJYWEYRHYFT8YTEGQBABJ43J')
album.artist // "Mushroomhead"
album.title // "The Righteous & The Butterfly"
album.year // 2014
album.genres // [ 'metal' ]
album.outOfPublication // true

我知道下面的代码可以获取它。问题是,在页面加载时,您如何知道redis-om提供的UID('01FJYWEYRHYFT8YTEGQBABJ43J')?

所有教程都使用redis insight来获取ID,但在实际项目中,你可能没有这个选项?

我唯一能想到的解决方案是自己生成UID,而不是使用redis-om提供的UID。这样我就可以获取它('ihavethisuid')。有人可以在这里分享见解或最佳实践吗?

或者我应该使用redis搜索吗?我不是做一个“Google”应用程序。也没有搜索栏。我只需要将Redis用作数据库。在页面加载时,它需要加载XYZ数据。就像Facebook一样。

英文:

I want to fetch a certain user from Redis database using redis-om

const album = await albumRepository.fetch('01FJYWEYRHYFT8YTEGQBABJ43J')
album.artist // "Mushroomhead"
album.title // "The Righteous & The Butterfly"
album.year // 2014
album.genres // [ 'metal' ]
album.outOfPublication // true

I know that the code below will fetch it. Problem is, how would you know the UID redis-om provides ('01FJYWEYRHYFT8YTEGQBABJ43J') on page load?

All tutorials use redis insight to get the id, but in a real life project. You wouldn’t have that?

The only solution i can think of is to make my own UID instead of the one redis-om provides. That way i can fetch it ('ihavethisuid'). Can someone please share insight or best practice here?

Or am I suppose to use redis search? I’m not doing a “google” app. Or a search bar. I just need redis as a database. On page load. It needs to load xyz data. Like Facebook.

答案1

得分: 0

当您使用Redis OM创建实体时,返回的对象上有entityId。您可以在那里获取它。

您提出的解决方案也是完全可行的,我自己也这样做过。对于像用户ID等情况,这种方法效果很好。请确保在模式中将其设置为'string'而不是'text'。

英文:

When you create an Entity with Redis OM, the object returned has the entityId on it. You can get it there.

Your suggested solution is also perfectly viable and I have done this myself. Works well for things like user IDs and whatnot. Be sure to make it a 'string' and not a 'text' in the Schema.

huangapple
  • 本文由 发表于 2023年6月19日 12:22:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定