使用MongoDb BulkWriteOperation抛出IllegalArgumentException进行批量upsert

时间:2015-04-13 22:13:22

标签: mongodb mongodb-java mongo-java mongo-java-driver

我正在尝试使用BulkWriteOperation进行批量upsert。

 public BulkWriteResult bulkUpsert(DomainName domainName, Map<DBObject, DBObject> dbObjectByQueryMap) {

  BulkWriteOperation bulkWriteOperation = mongoCollectionPool.collection(domainName).initializeOrderedBulkOperation();

  for (Map.Entry<DBObject, DBObject> entry : dbObjectByQueryMap.entrySet()) {
     bulkWriteOperation.find(entry.getKey()).upsert().updateOne(entry.getValue());
  }
  BulkWriteResult result = bulkWriteOperation.execute();
  if (result.getMatchedCount() <= 0)
     throw new DomainNotFoundException(String.format("Could not upsert %s .", domainName.toString()));

  return result;

}

映射具有Key as Query和Value作为DBObject(Java Bean)。这个方法抛出IlllegalArgumentException?。

这是StackTrace

java.lang.IllegalArgumentException: Invalid BSON field name code
at org.bson.AbstractBsonWriter.writeName(AbstractBsonWriter.java:494)
at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:127)
at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:61)
at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)
at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)
at com.mongodb.connection.UpdateCommandMessage.writeTheWrites(UpdateCommandMessage.java:83)
at com.mongodb.connection.UpdateCommandMessage.writeTheWrites(UpdateCommandMessage.java:42)
at com.mongodb.connection.BaseWriteCommandMessage.encodeMessageBody(BaseWriteCommandMessage.java:112)
at com.mongodb.connection.BaseWriteCommandMessage.encodeMessageBody(BaseWriteCommandMessage.java:35)
at com.mongodb.connection.RequestMessage.encode(RequestMessage.java:132)
at com.mongodb.connection.BaseWriteCommandMessage.encode(BaseWriteCommandMessage.java:89)
at com.mongodb.connection.WriteCommandProtocol.sendMessage(WriteCommandProtocol.java:170)
at com.mongodb.connection.WriteCommandProtocol.execute(WriteCommandProtocol.java:73)
at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:63)
at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:37)
at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:155)
at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:219)
at com.mongodb.connection.DefaultServerConnection.updateCommand(DefaultServerConnection.java:120)
at com.mongodb.operation.MixedBulkWriteOperation$Run$3.executeWriteCommandProtocol(MixedBulkWriteOperation.java:440)
at com.mongodb.operation.MixedBulkWriteOperation$Run$RunExecutor.execute(MixedBulkWriteOperation.java:604)
at com.mongodb.operation.MixedBulkWriteOperation$Run.execute(MixedBulkWriteOperation.java:361)
at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:148)
at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:141)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:186)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:177)
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:141)
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:72)
at com.mongodb.Mongo.execute(Mongo.java:745)
at com.mongodb.Mongo$2.execute(Mongo.java:728)
at com.mongodb.DBCollection.executeBulkWriteOperation(DBCollection.java:1968)
at com.mongodb.DBCollection.executeBulkWriteOperation(DBCollection.java:1962)
at com.mongodb.BulkWriteOperation.execute(BulkWriteOperation.java:98)

这是我的文档看起来像

{
     "code": "KRK",
     "countryCode": "IN",
     "latitude": 45.0667,
     "longitude": 14.6000,
    "timeZoneOffsetId": "507f1f77bcf86cd799439011"
}

查询是

new QueryBuilder().put("code").is("KRK").get()

Bean就是

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.openapi.rest.Airport;
import lombok.NoArgsConstructor;
import org.bson.BSONObject;
import java.util.Map;
import java.util.Set;
@NoArgsConstructor
public class MongoAirport implements Airport,DBObject {
    private DBObject realObject;
    public MongoAirport() {
     this.realObject = new BasicDBObject();
   }


private String countryCode;
   private Double latitude;
   private Double longitude;
   private String timeZoneOffsetId;
   private String code;
   @Override public String getCode() {
      return code;
   }
   @Override public void setCode(String code) {
      this.code = code;
   }
   @Override public String getCountryCode() {
      return countryCode;
   }
   @Override public void setCountryCode(String countryCode) {
      this.countryCode = countryCode;
   }

   @Override public Double getLatitude() {
      return latitude;
   }
   @Override public void setLatitude(Double latitude) {
      this.latitude = latitude;
   }
   @Override public Double getLongitude() {
      return longitude;
   }
   @Override public void setLongitude(Double longitude) {
      this.longitude = longitude;
   }
   @Override public String getTimeZoneOffsetId() {
      return timeZoneOffsetId;
   }
   @Override public void setTimeZoneOffsetId(String timeZoneOffsetId) {
      this.timeZoneOffsetId = timeZoneOffsetId;
   }
   @JsonIgnore
   @Override public void markAsPartialObject() {
      realObject.markAsPartialObject();
   }

   @JsonIgnore
   @Override public boolean isPartialObject() {
      return realObject.isPartialObject();
   }

   @JsonIgnore
   @Override public Object put(String key, Object v) {
      return realObject.put(key, v);
   }
   @JsonIgnore
   @Override public void putAll(BSONObject o) {
      realObject.putAll(o);
   }
   @JsonIgnore
   @Override public void putAll(Map m) {
      realObject.putAll(m);
   }

   @JsonIgnore
   @Override public Object get(String key) {
      return realObject.get(key);
   }
   @JsonIgnore
   @Override public Map toMap() {
      return realObject.toMap();
   }
   @JsonIgnore
   @Override public Object removeField(String key) {
      return realObject.removeField(key);
   }
   @JsonIgnore
   @Override public boolean containsKey(String s) {
      return realObject.containsKey(s);
   }
   @JsonIgnore
   @Override public boolean containsField(String s) {
      return realObject.containsField(s);
   }
   @JsonIgnore
   @Override public Set<String> keySet() {

      return realObject.keySet();
   }
}

你能帮我解决问题吗?。

1 个答案:

答案 0 :(得分:1)

我找到了问题的答案。 我需要更改for循环

for (Map.Entry<BasicDBObject, DBObject> entry : dbObjectByQueryMap.entrySet()) {
         bulkWriteOperation.find(entry.getKey()).upsert().updateOne(new BasicDBObject("$set", entry.getValue()));
      }

而不是

entry.getValue()

我添加了

new BasicDBObject("$set", entry.getValue())

`