英文:
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<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();
}
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<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();
}
It seems that the upsert
function is not really working as intended. Replacing it with replaceOne
worked.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论