批量操作 upsert 报错:无效的 BSON 字段名。

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

BulkOperations upsert throwing Invalid BSON field name

问题

以下是您的Java代码的翻译部分:

public void upsertAll(final List<StringEntry> strings) {
    BulkOperations bulkOps = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, StringEntry.class, DOCUMENT);
    List<Pair<Query, Update>> operations = new ArrayList<>();
    strings.forEach(string -> {
        Query query = new Query();
        query.addCriteria(Criteria.where("msgid").is(string.getMsgId()));  
        Document doc = new Document();
        mongoTemplate.getConverter().write(string, doc);
        Update update = Update.fromDocument(doc); 
        bulkOps.upsert(query, update);
    }); 
    bulkOps.execute();
}

异常的堆栈跟踪翻译如下:

java.lang.IllegalArgumentException: 无效的BSON字段名称msgid
    at org.bson.AbstractBsonWriter.writeName(AbstractBsonWriter.java:534)
    at com.mongodb.internal.connection.BsonWriterDecorator.writeName(BsonWriterDecorator.java:193)
    at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:211)
    at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:154)
    at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:45)
    at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)
    at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)
    at com.mongodb.internal.connection.SplittablePayload$WriteRequestEncoder.encode(SplittablePayload.java:221)
    at com.mongodb.internal.connection.SplittablePayload$WriteRequestEncoder.encode(SplittablePayload.java:187)

不包括代码部分,只提供了代码和异常堆栈跟踪的翻译。

英文:

Here is my java code

public void upsertAll(final List&lt;StringEntry&gt; strings) {
    BulkOperations bulkOps = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, StringEntry.class, DOCUMENT);
    List&lt;Pair&lt;Query, Update&gt;&gt; operations = new ArrayList&lt;&gt;();
    strings.forEach(string -&gt; {
        Query query = new Query();
        query.addCriteria(Criteria.where(&quot;msgid&quot;).is(string.getMsgId()));  
        Document doc = new Document();
        mongoTemplate.getConverter().write(string, doc);
        Update update = Update.fromDocument(doc); 
        bulkOps.upsert(query, update);
    }); 
    bulkOps.execute();
}

Exception from stacktrace.

    java.lang.IllegalArgumentException: Invalid BSON field name msgid
        at org.bson.AbstractBsonWriter.writeName(AbstractBsonWriter.java:534)
        at com.mongodb.internal.connection.BsonWriterDecorator.writeName(BsonWriterDecorator.java:193)
        at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:211)
        at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:154)
        at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:45)
        at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)
        at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)
        at com.mongodb.internal.connection.SplittablePayload$WriteRequestEncoder.encode(SplittablePayload.java:221)
        at com.mongodb.internal.connection.SplittablePayload$WriteRequestEncoder.encode(SplittablePayload.java:187)

How can I get the bulk upsert function to work?

答案1

得分: 1

在查看了该库的源代码和测试之后,我已经成功解决了我的问题。BulkOperationsIntegrationTest

public void upsertAll(final List<StringEntry> strings) {
    FindAndReplaceOptions options = FindAndReplaceOptions.options();
    BulkOperations bulkOps = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, StringEntry.class, DOCUMENT);
    List<Pair<Query, Update>> operations = new ArrayList<>();
    for(StringEntry string: strings) {
        Query query = new Query();
        query.addCriteria(Criteria.where("msgid").is(string.getMsgId()));  
        Document doc = new Document();
        mongoTemplate.getConverter().write(string, doc);
        bulkOps = bulkOps.replaceOne(query, doc, options.upsert()); 
    }
    bulkOps.execute();
}

看起来 upsert 函数并没有按预期工作。将其替换为 replaceOne 就有效了。

英文:

After looking through the source code and tests of the library, I have managed to solve my problem. BulkOperationsIntegrationTest

public void upsertAll(final List&lt;StringEntry&gt; strings) {
    FindAndReplaceOptions options = FindAndReplaceOptions.options();
    BulkOperations bulkOps = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, StringEntry.class, DOCUMENT);
    List&lt;Pair&lt;Query, Update&gt;&gt; operations = new ArrayList&lt;&gt;();
    for(StringEntry string: strings) {
        Query query = new Query();
        query.addCriteria(Criteria.where(&quot;msgid&quot;).is(string.getMsgId()));  
        Document doc = new Document();
        mongoTemplate.getConverter().write(string, doc);
        bulkOps = bulkOps.replaceOne(query, doc, options.upsert()); 
    }
    bulkOps.execute();
}

It seems that the upsert function is not really working as intended. Replacing it with replaceOne worked.

huangapple
  • 本文由 发表于 2020年9月28日 17:49:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64099812.html
匿名

发表评论

匿名网友

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

确定