Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongodb Panache Update query won't work #38405

Closed
anamak9 opened this issue Jan 25, 2024 · 2 comments · Fixed by #44225
Closed

Mongodb Panache Update query won't work #38405

anamak9 opened this issue Jan 25, 2024 · 2 comments · Fixed by #44225
Labels
Milestone

Comments

@anamak9
Copy link

anamak9 commented Jan 25, 2024

Describe the bug

Panache Update query on MongoEntity is not working as expected.

Expected behavior

The YourMongoEntity should be updated for the given fields and the method should return the value of 1L (because only one document should match with _id value).

Actual behavior

 java.lang.IllegalArgumentException: All update operators must start with '$', but 'someField' does not
        at org.bson.AbstractBsonWriter.writeName(AbstractBsonWriter.java:535)
        at com.mongodb.internal.connection.BsonWriterDecorator.writeName(BsonWriterDecorator.java:196)
        at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:163)
        at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:44)
        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:216)
        at com.mongodb.internal.connection.SplittablePayload$WriteRequestEncoder.encode(SplittablePayload.java:182)
        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:77)
        at com.mongodb.internal.connection.BsonWriterHelper.writePayload(BsonWriterHelper.java:59)
        at com.mongodb.internal.connection.CommandMessage.encodeMessageBodyWithMetadata(CommandMessage.java:162)
        at com.mongodb.internal.connection.RequestMessage.encode(RequestMessage.java:136)
        at com.mongodb.internal.connection.CommandMessage.encode(CommandMessage.java:59)
        at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:360)
        at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:114)
        at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:765)
        at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:76)
        at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:209)
        at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:115)
        at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:83)
        at com.mongodb.internal.connection.DefaultServer$OperationCountTrackingConnection.command(DefaultServer.java:307)
        at com.mongodb.internal.operation.MixedBulkWriteOperation.executeCommand(MixedBulkWriteOperation.java:395)
        at com.mongodb.internal.operation.MixedBulkWriteOperation.executeBulkWriteBatch(MixedBulkWriteOperation.java:259)
        at com.mongodb.internal.operation.MixedBulkWriteOperation.lambda$execute$2(MixedBulkWriteOperation.java:203)
        at com.mongodb.internal.operation.SyncOperationHelper.lambda$withSourceAndConnection$0(SyncOperationHelper.java:127)
        at com.mongodb.internal.operation.SyncOperationHelper.withSuppliedResource(SyncOperationHelper.java:152)
        at com.mongodb.internal.operation.SyncOperationHelper.lambda$withSourceAndConnection$1(SyncOperationHelper.java:126)
        at com.mongodb.internal.operation.SyncOperationHelper.withSuppliedResource(SyncOperationHelper.java:152)
        at com.mongodb.internal.operation.SyncOperationHelper.withSourceAndConnection(SyncOperationHelper.java:125)
        at com.mongodb.internal.operation.MixedBulkWriteOperation.lambda$execute$3(MixedBulkWriteOperation.java:188)
        at com.mongodb.internal.operation.MixedBulkWriteOperation.lambda$decorateWriteWithRetries$0(MixedBulkWriteOperation.java:146)
        at com.mongodb.internal.async.function.RetryingSyncSupplier.get(RetryingSyncSupplier.java:67)
        at com.mongodb.internal.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:207)
        at com.mongodb.internal.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:77)
        at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:173)
        at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:1085)
        at com.mongodb.client.internal.MongoCollectionImpl.executeUpdate(MongoCollectionImpl.java:1069)
        at com.mongodb.client.internal.MongoCollectionImpl.updateMany(MongoCollectionImpl.java:632)
        at com.mongodb.client.internal.MongoCollectionImpl.updateMany(MongoCollectionImpl.java:627)
        at io.quarkus.mongodb.panache.common.runtime.PanacheUpdateImpl.executeUpdate(PanacheUpdateImpl.java:61)
        at io.quarkus.mongodb.panache.common.runtime.PanacheUpdateImpl.where(PanacheUpdateImpl.java:34)

How to Reproduce?

Try the following:

Document updDocument = new Document("somefield", someValue);
YourMongoEntity.update(updDocument)
                .where("_id = ?1", new ObjectId(id)); // tried using "id" instead of "_id" or .where(new Document(....)) but still didn't 
work

Output of uname -a or ver

No response

Output of java -version

Java version: 21.0.1, vendor: Oracle Corporation, runtime: /usr/lib/jvm/jdk-21-oracle-x64

Quarkus version or git rev

3.0.3.Final

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.9.5 (57804ffe001d7215b5e7bcb531cf83df38f93546)

Additional information

No response

@anamak9 anamak9 added the kind/bug Something isn't working label Jan 25, 2024
Copy link

quarkus-bot bot commented Jan 25, 2024

/cc @FroMage (panache), @loicmathieu (mongodb,panache)

@loicmathieu
Copy link
Contributor

@anamak9 there are two ways to update entities in MongodDB with Panache:

  1. By updating the entity object: you update the field then call myEntity.update(). This mandates that you have an instance of your entity at hand (loaded for ex via findById).
  2. By using an update query.

Here, you want to do a single update by the ID of a document by using an update query which is more performant if you don't have an instance of your entity but less easy / idiomatic.

Maybe the documentation is not very clear but when you use MyEntity.update(Document).where(...) the document must be an update document (something like {$set: {myField: myValue}} as this is not very easy to use MongoDB with Panache provides a simplified DSL what is called PanacheQL.

To fix your example, you can use something like:

Document updDocument = new Document("$set", new Document("somefield", someValue));
YourMongoEntity.update(updDocument)
                .where("_id = ?1", new ObjectId(id));

Or in a more simple and Panache idiomatic way:

YourMongoEntity.update("somefield", "someValue")
                .where("_id = ?1", new ObjectId(id));

In this case, Panache will translate the update parameters to a document for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants