MongoDB-转义点 '.'在地图键中]

2024-02-06

产品的地图键代码包含点,但未配置替换!首先确保地图键不包含点或配置适当的替换!

org.springframework.data.mapping.model.MappingException:地图键 foo.bar.key 包含点,但未配置替换!首先确保地图键不包含点或配置适当的替换! 在org.springframework.data.mongodb.core.convert.MappingMongoConverter.潜在EscapeMapKey(MappingMongoConverter.java:622) 在org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeMapInternal(MappingMongoConverter.java:586) 在 org.springframework.data.mongodb.core.convert.MappingMongoConverter.createMap(MappingMongoConverter.java:517) 在org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:424) 在 org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:386) 在 org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:373) 在org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:257) 在org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:373) 在org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:451) 在 org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:386) 在 org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:373) 在org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:257) 在org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:373) 在org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:451) 在 org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:386) 在 org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:373) 在org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:257) 在org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:373) 在org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:345) 在 org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:310) 在 org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:77) 在 org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:859) 在 org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:806) 在 org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:794)

当我们尝试插入值时,就会发生这种情况。我们该如何解决这个问题呢?

这是我的课

@Configuration
@EnableMongoRepositories("net.ooo.hepsiburada.**.repository")
@Profile(Constants.SPRING_PROFILE_CLOUD)
public class CloudMongoDbConfiguration extends AbstractMongoConfiguration  {

    private final Logger log = LoggerFactory.getLogger(CloudDatabaseConfiguration.class);

    @Inject
    private MongoDbFactory mongoDbFactory;

    @Bean
    public ValidatingMongoEventListener validatingMongoEventListener() {
        return new ValidatingMongoEventListener(validator());
    }

    @Bean
    public LocalValidatorFactoryBean validator() {
        return new LocalValidatorFactoryBean();
    }

    @Bean
    public CustomConversions customConversions() {
        List<Converter<?, ?>> converterList = new ArrayList<>();;
        converterList.add(DateToZonedDateTimeConverter.INSTANCE);
        converterList.add(ZonedDateTimeToDateConverter.INSTANCE);
        converterList.add(DateToLocalDateConverter.INSTANCE);
        converterList.add(LocalDateToDateConverter.INSTANCE);
        converterList.add(DateToLocalDateTimeConverter.INSTANCE);
        converterList.add(LocalDateTimeToDateConverter.INSTANCE);
        return new CustomConversions(converterList);
    }

    @Override
    protected String getDatabaseName() {
        return mongoDbFactory.getDb().getName();
    }

    @Override
    public Mongo mongo() throws Exception {
        return mongoDbFactory().getDb().getMongo();
    }
}

使用 Spring Data MongoDB 时,您将获得以下实例:org.springframework.data.mongodb.core.convert.MappingMongoConverter具有mapKeyDotReplacement默认设置为 null - 这就是您收到异常的原因。

您需要创建自己的实例org.springframework.data.mongodb.core.convert.MappingMongoConverter或者只是使用其提供者 setter 方法修改现有实例:

/**
 * Configure the characters dots potentially contained in a {@link Map} shall be replaced with. By default we don't do
 * any translation but rather reject a {@link Map} with keys containing dots causing the conversion for the entire
 * object to fail. If further customization of the translation is needed, have a look at
 * {@link #potentiallyEscapeMapKey(String)} as well as {@link #potentiallyUnescapeMapKey(String)}.
 * 
 * @param mapKeyDotReplacement the mapKeyDotReplacement to set
 */
public void setMapKeyDotReplacement(String mapKeyDotReplacement) {
    this.mapKeyDotReplacement = mapKeyDotReplacement;
}

在 MongoDB 中,点始终被视为特殊字符,因此避免使用它很可能会在将来为您省去一些其他麻烦。

编辑: 覆盖默认值MappingMongoConverter添加以下 bean 声明:

  @Bean
  public MappingMongoConverter mongoConverter(MongoDbFactory mongoFactory) throws Exception {
    DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoFactory);
    MappingMongoConverter mongoConverter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
    mongoConverter.setMapKeyDotReplacement(".");

    return mongoConverter;
  }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MongoDB-转义点 '.'在地图键中] 的相关文章

随机推荐

  • Linq .Contains 包含大集合会导致 TDS 错误

    我有点过于简单化了 因为我正在寻找通用的答案 假设我有一个这样的表设置 Parent recno int unique pk date datetime stuff varchar 50 Child parentrecno int fk P
  • Android Studio 调试错误“连接被拒绝”

    我知道以前有人问过这个问题 但我尝试了所有建议的解决方案 但还没有解决我的问题 我无法使用 Mac Yosemite 调试任何 Android studio 1 5 预览版 2 应用程序 我得到的错误是 Unable to open deb
  • 建议像 google 一样使用 postgresql trigrams 和全文搜索

    我想做一个文本搜索 我在用着PostgreSQL http www postgresql org 因为神奇的Postgis http postgis refractions net 我正在考虑使用FTS http www postgresq
  • Aarch64 上 C++11 原子的部分重新排序

    我正在看gcc 的 rmw 原子的编译器输出 https goo gl ZWLeCJ并注意到一些奇怪的事情 在 Aarch64 上 诸如 fetch add 之类的 rmw 操作可以通过宽松的负载进行部分重新排序 在 Aarch64 上 可
  • 获取磁盘上文件的所有者

    下面是我当前的宏 它运行得很好 但我想在 Cells r 10 中添加文件的所有者 这样的事情怎么可能完成呢 我找不到像 File Owner 之类的命令 Sub DoFolder Folder If Ans vbNo Then GoTo
  • Elasticsearch - 匹配字符串或空值

    我必须检查某个字段是否与特定文本匹配或为空 可以这样做吗 谢谢 您可以通过使用来实现这一点missing http www elasticsearch org guide en elasticsearch reference current
  • TypeScript 接口数组类型错误 TS2411

    我很难理解这一点 支持的索引类型有两种 字符串和数字 可以支持两种类型的索引 但限制是从数字索引返回的类型必须是从字符串索引返回的类型的子类型 虽然索引签名是描述数组和 字典 模式的强大方法 但它们还强制所有属性与其返回类型相匹配 在此示例
  • Android WebRTC 中的本地视频渲染器

    我正在使用这个库 https bintray com google webrtc google webrtc https bintray com google webrtc google webrtc 我想要实现的目标 至少在我的项目开始时
  • 共享库如何知道它所在的位置?

    我正在为 Linux 机器开发一个共享库 它是通过 rpath 相对于主可执行文件动态加载的 现在 库本身尝试相对于其位置动态加载其他库 但没有 rpath 我使用 scandir 来搜索某个文件夹中的共享库 我还不知道它们的名称 仅当工作
  • 正则表达式匹配不包含任何超过 10 个字符的单词的字符串?

    S 10 我有一个正则表达式 它将匹配任何包含 10 个字符的单词的字符串 然而我需要它的反面 一个正则表达式 仅匹配不包含 gt 10 个字符的单词的字符串 使用否定断言 S 10 S 10 匹配 10 的序列 S 它必须是更长的子序列
  • Spring:如何将属性文件中的值传递给构造函数

    我有一个MongoService类为 public class MongoService private final Mongo mongo private final String database private static fina
  • 我想在 SwiftUI 中的 TextField 添加 $ 符号

    Hi I want to add sign to a TextField when a user is typing 这是我当前的代码 ZStack alignment leading if price isEmpty Text Enter
  • fftw c2c:转换后的真实数据中缺少对称性

    最近我遇到了一些关于fftw的使用及其c2c转换的问题 参见 3d c2c fft 与 fftw 库 https stackoverflow com questions 10374656 3d c2c fft with fftw libra
  • 特征向量变换的差异:Mathematica 与 SciPy

    类似的问题之前曾在这里被问过 但似乎没有人回答我的例子 我使用 Mathematica 和 SciPy 计算矩阵 A 的特征值和特征向量 特征值一致 但特征向量则不然 1 最低 特征值 特征向量一致 2 Mathematica 和 SciP
  • Python3解析xml

    我尝试使用不同的 python3 模块和互联网上的不同文章来解析 XML 但没有成功 我有这个 XML
  • bootstrap 3 - 如何将品牌放置在导航栏的中心?

    我正在使用 Bootstrap 3 我想要一个仅包含品牌的导航栏 没有其他链接或任何其他内容 我希望品牌位于中心 我怎样才能做到这一点 以下 CSS 不起作用 navbar brand text align center css navba
  • Firebase 运行循环中未捕获的异常 (3.0.0)

    我正在使用最新的 firebase 9 0 2 build gradle dependencies compile com google firebase firebase database 9 0 2 compile com google
  • opencv中的错误 - python3 (detectMultiScale) 人脸识别

    我正在尝试创建一个人脸检测程序 当我执行以下代码时 import numpy as np import cv2 face cascade cv2 CascadeClassifier Practica Proyectos Practica O
  • 甜甜圈缓存 ASP.NET MVC2

    是否有可能使用 MVC2 进行甜甜圈缓存 即使进行了大量研究 我也无法找到可行的解决方案 甜甜圈缓存不适用于 ASP NET MVC 2 请参阅http forums asp net t 1521502 aspx http forums a
  • MongoDB-转义点 '.'在地图键中]

    产品的地图键代码包含点 但未配置替换 首先确保地图键不包含点或配置适当的替换 org springframework data mapping model MappingException 地图键 foo bar key 包含点 但未配置替