【问题标题】:How can I ignore a "$" in a DocumentContent to save in MongoDB?如何忽略 DocumentContent 中的“$”以保存在 MongoDB 中?
【发布时间】:2018-09-18 15:02:33
【问题描述】:

我的问题是,如果我在内容中保存带有 $ 的文档,Mongodb 会给我一个例外:

java.lang.IllegalArgumentException: 无效的 BSON 字段名称 $xxx

我希望 mongodb 忽略内容中的 $ 字符。

我的应用程序是用 java 编写的。我读取文件的内容并将其作为字符串放入对象中。之后,该对象将使用 MongoRepository 类保存。

有人有什么想法吗??

Example content

编辑:我听说 mongodb 也有同样的问题。我们的 Springboot 发现我使用 dot 解决方法,但不是为了美元。

How to configure mongo converter in spring to encode all dots in the keys of map being saved in mongo db

【问题讨论】:

  • 您的数据是什么样的?您是如何尝试保存它的?
  • 我用 Java 编写所有程序。我创建了一个自己创建的对象,并将带有 OCR 的文件的内容读取到字符串中,并将其添加到对象中。之后,我使用 MongoRepository 类保存对象。不知何故,mongodb 使用 $ 作为更新运算符命令读取字符串。

标签: mongodb


【解决方案1】:

如果您使用的是 Spring Boot,您可以扩展 MappingMongoConverter 类并添加执行转义/取消转义的覆盖方法。

@Component
public class MappingMongoConverterCustom extends MappingMongoConverter {

    protected @Nullable
    String mapKeyDollarReplacemant = "characters_to_replace_dollar";

    protected @Nullable
    String mapKeyDotReplacement = "characters_to_replace_dot";

    public MappingMongoConverterCustom(DbRefResolver dbRefResolver, MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {
        super(dbRefResolver, mappingContext);
    }

    @Override
    protected String potentiallyEscapeMapKey(String source) {
        if (!source.contains(".") && !source.contains("$")) {
            return source;
        }

        if (mapKeyDotReplacement == null && mapKeyDollarReplacemant == null) {
            throw new MappingException(String.format(
                    "Map key %s contains dots or dollars but no replacement was configured! Make "
                            + "sure map keys don't contain dots or dollars in the first place or configure an appropriate replacement!",
                    source));
        }

        String result = source;
        if(result.contains(".")) {
            result = result.replaceAll("\\.", mapKeyDotReplacement);
        }
        if(result.contains("$")) {
            result = result.replaceAll("\\$", mapKeyDollarReplacemant);
        }
        
        //add any other replacements you need

        return result;
    }

    @Override
    protected String potentiallyUnescapeMapKey(String source) {
        String result = source;
        if(mapKeyDotReplacement != null) {
            result = result.replaceAll(mapKeyDotReplacement, "\\.");
        }
        if(mapKeyDollarReplacemant != null) {
            result = result.replaceAll(mapKeyDollarReplacemant, "\\$");
        }
        
         //add any other replacements you need
        return result;
    }
}

如果您采用这种方法,请确保您从 AbstractMongoConfiguration 覆盖默认转换器,如下所示:

@Configuration
public class MongoConfig extends AbstractMongoConfiguration{

@Bean
public DbRefResolver getDbRefResolver() {
    return new DefaultDbRefResolver(mongoDbFactory());
}

@Bean
@Override
public MappingMongoConverter mappingMongoConverter() throws Exception {
    MappingMongoConverterCustom converter = new MappingMongoConverterCustom(getDbRefResolver(), mongoMappingContext());
    converter.setCustomConversions(customConversions());

    return converter;
}

   .... whatever you might need extra ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 2014-11-12
    相关资源
    最近更新 更多