java,Morphia 如何在使用 Morphia 查找方法时比较字符串

发布于 2024-12-06 14:41:13 字数 609 浏览 0 评论 0原文

我对此很陌生,所以就开始吧。
尝试从 MongoDb 获取名为“Bob”的用户。

我有:

UserData ud = MonConMan.instance().getDb().find(UserData.class, "name","bob").get();

如果“bob”大写“Bob”,则无法找到它。
我知道我可以获取 List 并执行 equalsIgnoreCase
我可以使用一些运算符吗?

我有用户登录,必须测试他们是否已注册。用户可以以任何他喜欢的方式输入他的名字,因此必须找到一种 equalsIgnoreCase 的方法。是的,这是一个问题,如果有大约 10,000 个名称,我无法获取所有名称并执行 equalsIgnoreCase 操作。当然,最初可以将所有用户名保存为小写,但这会破坏名称的视觉外观。

查看 wiki 但看不到任何内容。

http://code.google.com/p /morphia/wiki/查询

Im new to this so here goes.
Trying to get a user called "Bob" from the MongoDb.

I have the:

UserData ud = MonConMan.instance().getDb().find(UserData.class, "name","bob").get();

The "bob" cannot be found if it has capital "Bob".
I understand i can get a List and do equalsIgnoreCase but are
there some Operators i can use?

I have users logging on and must test to see if they are registered. A user can type his name anyway he likes so must find a way to equalsIgnoreCase. Yea this is a problem, i cannot get all names and do equalsIgnoreCase, if there are like 10,000. One could of course initially save all user names in lowercase but that would destroy the visual appearance of the name.

looking at the wiki but cannot see any..

http://code.google.com/p/morphia/wiki/Query

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

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

发布评论

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

评论(3

数理化全能战士 2024-12-13 14:41:13

使用java正则表达式,就像这样。

String name = "bob";
Pattern pattern = Pattern.compile("^" + bob + "$", Pattern.CASE_INSENSITIVE);//This line will create a pattern to match words starts with "b", ends with "b" and its case insensitive too.

Query<UserData> query = createQuery().field("name").equal(pattern).retrievedFields(true, "id");//Replace `id` with what ever name you use in UserData for '_id'
UserData user = query.get();
if(user!=null){
  //he is already registered
}
else{
//He is  a new guy

}

(我不擅长正则表达式,所以您可能已经阅读过$&^某处。)

您应该确保用户名您用来验证新用户在整个系统中应该是唯一的。

Use java regex, like this.

String name = "bob";
Pattern pattern = Pattern.compile("^" + bob + "$", Pattern.CASE_INSENSITIVE);//This line will create a pattern to match words starts with "b", ends with "b" and its case insensitive too.

Query<UserData> query = createQuery().field("name").equal(pattern).retrievedFields(true, "id");//Replace `id` with what ever name you use in UserData for '_id'
UserData user = query.get();
if(user!=null){
  //he is already registered
}
else{
//He is  a new guy

}

(I am not good at regex, so you may have read about$&^somewhere. )

You should be sure that the user names you are using to validate a new user should be unique across your system.

白芷 2024-12-13 14:41:13

最终保留了两个字段,例如
- 小写用户名
- 原始用户名

这样我可以使用小写用户名搜索用户

Ended up keeping two fields like
- lowercaseusername
- originalusername

This way i could search for a user using the lowercaseusername

孤独岁月 2024-12-13 14:41:13

您可以使用以下代码查找 UserData 的名称:

Query<UserData> query = createQuery().filter("name","bob");
find(query);

在我的应用程序中,此代码返回所有具有字段 nameUserData ,其中“鲍勃”值。
代码也可以是这样的:

Query<UserData> query = createQuery().field("name").equal("bob");
find(query);

这些代码将位于扩展 BasicDaoUserDataDao 中,并在构造函数中从吗啡接收数据存储。

You can make find a name of a UserData using this code :

Query<UserData> query = createQuery().filter("name","bob");
find(query);

In my application, this code return all UserData that haves a field name with "bob" value.
The code can be this way too :

Query<UserData> query = createQuery().field("name").equal("bob");
find(query);

These codes will be in a UserDataDao that extends BasicDao, and receives in the construtor the datastore from morphia.

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