首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Data (mongo DB)持久化布尔实体属性的可读值

Spring Data (mongo DB)持久化布尔实体属性的可读值
EN

Stack Overflow用户
提问于 2018-05-30 20:22:37
回答 1查看 298关注 0票数 0

我目前在一个原型项目中工作,我们在一个非常有限的数据集上使用mongo db正则表达式查询,以执行“包含”搜索来执行全文搜索。因为我们的实体中有一些布尔型属性,所以我们希望持久化可读的、因此可搜索的字符串值(作为布尔JSON表示的附加或替代)。

假设我的实体看起来像这样:

代码语言:javascript
复制
public class Foo {
  protected Boolean superPowers;
  protected Boolean evil;
  //some transitive persistence thingy
}

在mongo db中,我想要这样的东西。

代码语言:javascript
复制
{
  //omitting things for brevity here
  superPowers: true,
  evil: false,
  description: "Super Hero" 
}

代码语言:javascript
复制
{
  //omitting things for brevity here
  superPowers: true,
  evil: true,
  description: "Villain" 
}

或者,现在也可以像这样(所以不计算组合)

代码语言:javascript
复制
{
  //omitting things for brevity here
  superPowers: "super powers", // if "true"
  evil: "", // if false it is an empty string
}

多谢你们的支持!

致以敬意,

索本

EN

回答 1

Stack Overflow用户

发布于 2018-05-30 21:07:58

您可以通过侦听BeforeSaveEvent来增加要编写的文档,在该and中,您将获得对域对象和从该域对象派生的文档的访问权限。

代码语言:javascript
复制
@Component
class AugmentingEventListener extends AbstractMongoEventListener<Foo> {

  public void onBeforeSaveEvent(BeforeSaveEvent<Foo> event) {
    // add fields to the document as needed
  }
}

在我们的reference documentation中可以找到更多关于这方面的信息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50604597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档