gpt4 book ai didi

java - MongoDb upsert 异常无效的 BSON 字段

转载 作者:IT老高 更新时间:2023-10-28 13:23:53 24 4
gpt4 key购买 nike

这个异常(exception):

Exception in thread "Thread-1" java.lang.IllegalArgumentException: Invalid BSON field name id
at org.bson.AbstractBsonWriter.writeName(AbstractBsonWriter.java:516)
at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:188)
at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:131)
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.connection.UpdateCommandMessage.writeTheWrites(UpdateCommandMessage.java:85)
at com.mongodb.connection.UpdateCommandMessage.writeTheWrites(UpdateCommandMessage.java:43)
at com.mongodb.connection.BaseWriteCommandMessage.encodeMessageBodyWithMetadata(BaseWriteCommandMessage.java:129)
at com.mongodb.connection.RequestMessage.encodeWithMetadata(RequestMessage.java:160)
at com.mongodb.connection.WriteCommandProtocol.sendMessage(WriteCommandProtocol.java:220)
at com.mongodb.connection.WriteCommandProtocol.execute(WriteCommandProtocol.java:101)
at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:64)
at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:37)
at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:168)
at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:289)
at com.mongodb.connection.DefaultServerConnection.updateCommand(DefaultServerConnection.java:143)
at com.mongodb.operation.MixedBulkWriteOperation$Run$3.executeWriteCommandProtocol(MixedBulkWriteOperation.java:490)
at com.mongodb.operation.MixedBulkWriteOperation$Run$RunExecutor.execute(MixedBulkWriteOperation.java:656)
at com.mongodb.operation.MixedBulkWriteOperation$Run.execute(MixedBulkWriteOperation.java:409)
at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:177)
at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:168)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:422)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:413)
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:168)
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:74)
at com.mongodb.Mongo.execute(Mongo.java:845)
at com.mongodb.Mongo$2.execute(Mongo.java:828)
at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:550)
at com.mongodb.MongoCollectionImpl.update(MongoCollectionImpl.java:542)
at com.mongodb.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:381)
at org.hpms.gis.MongoDbTest.insert(MongoDbTest.java:63)
at java.lang.Thread.run(Thread.java:748)

由以下代码抛出:

     final UUID     id        = UUID.randomUUID();
final double frequency = 8_000.0 + ( 10_000.0 * Math.random() );
final double startSec = 100*i;
final double startNano = 10*i;
final double endSec = startSec + ( 100*i );
final double endNano = startNano + ( 10*i );
final double latitude = ( 180.0*Math.random() ) - 90.0;
final double longitude = ( 360.0*Math.random() ) - 180.0;
final Document trackID = new Document(
"id", new Document(
"msb", id.getMostSignificantBits ()) .append(
"lsb", id.getLeastSignificantBits()));
final Document track = new Document(
"id", new Document(
"msb", id.getMostSignificantBits ()) .append(
"lsb", id.getLeastSignificantBits())).append(
"frequency", frequency ) .append(
"start", new Document(
"seconds", startSec ) .append(
"nanoSec", startNano )).append(
"end", new Document(
"seconds", endSec ) .append(
"nanoSec", endNano )).append(
"position", new Document(
"latitude" , latitude ) .append(
"longitude", longitude )).append(
"padding", new byte[1000-8-8-8-4-4-4-4-8-8] );
//_collection.insertOne( track );
_collection.updateOne(
trackID,
track,
new UpdateOptions().upsert( true ));

注释代码 _collection.insertOne( track ); 执行良好。

为什么插入没问题,而“upsert”却不行?

最佳答案

updateOne 用于使用更新运算符更新文档字段。您需要 replaceOne 来获取替换文档。

_collection.replaceOne(
trackID,
track,
new UpdateOptions().upsert( true ));

这里有更多

更新运算符(operator):https://docs.mongodb.com/manual/reference/operator/update-field/

更新一:https://docs.mongodb.com/manual/reference/method/db.collection.updateOne/

替换一个:https://docs.mongodb.com/manual/reference/method/db.collection.replaceOne/

关于java - MongoDb upsert 异常无效的 BSON 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43711716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com