gpt4 book ai didi

java - 我的 java 应用程序抛出一个异常,显示 Invalid BSON field name Name

转载 作者:行者123 更新时间:2023-12-02 09:14:39 25 4
gpt4 key购买 nike

我有这个代码:

public class UpdateShoe {

MongoClient mongoClient = MongoClients.create();
MongoDatabase db = mongoClient.getDatabase("MirandasShoes");
MongoCollection<Document> collection = db.getCollection("Shoes");
MongoCursor<Document> cursor = collection.find().iterator();

public void UpdateShoe(Shoe sfilter, Shoe snew) {
Document sfilterdoc = new Document();
sfilterdoc.append("_id", sfilter.getId());
sfilterdoc.append("Name", sfilter.getName());
sfilterdoc.append("Brand", sfilter.getBrand());
sfilterdoc.append("Type", sfilter.getType());
sfilterdoc.append("Color", sfilter.getColor());
sfilterdoc.append("Size", sfilter.getSize());
sfilterdoc.append("Price", sfilter.getPrice());

Document snewdoc = new Document();
snewdoc.append("Name", snew.getName());
snewdoc.append("Brand", snew.getBrand());
snewdoc.append("Type", snew.getType());
snewdoc.append("Color", snew.getColor());
snewdoc.append("Size", snew.getSize());
snewdoc.append("Price", snew.getPrice());

this.collection.updateOne(sfilterdoc, snewdoc);
}

}

当我点击更新按钮时,它抛出异常,显示无效的 BSON 字段名称名称。在智能感知中,它指定参数必须是 bson 并且我正在使用文档,但在在线文档中主要使用文档,那么为什么它会抛出异常,我的代码中有什么问题吗?

这是我点击更新按钮时的控制台输出:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Invalid BSON field name Name
at org.bson.AbstractBsonWriter.writeName(AbstractBsonWriter.java:532)
at com.mongodb.internal.connection.BsonWriterDecorator.writeName(BsonWriterDecorator.java:193)
at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:198)
at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:141)
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.operation.BulkWriteBatch$WriteRequestEncoder.encode(BulkWriteBatch.java:403)
at com.mongodb.operation.BulkWriteBatch$WriteRequestEncoder.encode(BulkWriteBatch.java:375)
at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)
at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)
at com.mongodb.internal.connection.BsonWriterHelper.writeDocument(BsonWriterHelper.java:75)
at com.mongodb.internal.connection.BsonWriterHelper.writePayload(BsonWriterHelper.java:59)
at com.mongodb.internal.connection.CommandMessage.encodeMessageBodyWithMetadata(CommandMessage.java:147)
at com.mongodb.internal.connection.RequestMessage.encode(RequestMessage.java:138)
at com.mongodb.internal.connection.CommandMessage.encode(CommandMessage.java:61)
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:248)
at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:99)
at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:450)
at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:72)
at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:218)
at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:269)
at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:131)
at com.mongodb.operation.MixedBulkWriteOperation.executeCommand(MixedBulkWriteOperation.java:435)
at com.mongodb.operation.MixedBulkWriteOperation.executeBulkWriteBatch(MixedBulkWriteOperation.java:261)
at com.mongodb.operation.MixedBulkWriteOperation.access$700(MixedBulkWriteOperation.java:72)
at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:205)
at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:196)
at com.mongodb.operation.OperationHelper.withReleasableConnection(OperationHelper.java:501)
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:196)
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:71)
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:206)
at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:1048)
at com.mongodb.client.internal.MongoCollectionImpl.executeUpdate(MongoCollectionImpl.java:1032)
at com.mongodb.client.internal.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:617)
at com.mongodb.client.internal.MongoCollectionImpl.updateOne(MongoCollectionImpl.java:612)
at DAL.UpdateShoe.UpdateShoe(UpdateShoe.java:45)
at UI.UpdateShoeUI.btnUpdateShoeActionPerformed(UpdateShoeUI.java:162)
at UI.UpdateShoeUI.access$100(UpdateShoeUI.java:18)
at UI.UpdateShoeUI$2.actionPerformed(UpdateShoeUI.java:79)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6539)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6304)
at java.awt.Container.processEvent(Container.java:2239)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2297)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
at java.awt.Container.dispatchEventImpl(Container.java:2283)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
at java.awt.EventQueue$4.run(EventQueue.java:733)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

最佳答案

您提到的代码相当于:

db.col.updateOne({"_id":"id"...etc.},{"name":"newName"...etc.}) 

您在这里缺少 $set 命令。所以你需要做的是:

Document snewDoc = new Document();
snewdoc.append("Name", snew.getName());
snewdoc.append("Brand", snew.getBrand());
snewdoc.append("Type", snew.getType());
snewdoc.append("Color", snew.getColor());
snewdoc.append("Size", snew.getSize());
snewdoc.append("Price", snew.getPrice());
Document updateDoc = new Document("$set",snewDoc);
this.collection.updateOne(sfilterdoc, updateDoc);

上面等价的正确格式是:

db.col.updateOne({"_id":"id"...etc.},{"$set":{"name":"newName"...etc.}})

关于java - 我的 java 应用程序抛出一个异常,显示 Invalid BSON field name Name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59094587/

25 4 0