0

Summary - It seems as if all examples to use Mongodb commands within java start with db.collection.someMongoMethod(). For example db.collection.update() or db.collection.updateMany(). I have not found any description on the web as to what db.collection represents or how to create this object(s). It is just assumed that I should know.

Problem - How do I get db.collection.foo() to resolve in Java? I would like to run the db.collection.updateMany() or any other mongodb method within a java method. Below are my sample method attempts with resulting compile errors shown. And the one method I got compile which failed at runtime. The runtime error will follow after the code block.

Methods Attempted -

public void renameField(MongoDatabase db, MongoCollection<org.bson.Document> bsonCollection, String oldField, String newField) {
        
        // Students cannot be resolved or is not a field
        //db.students.update({oldField: {$exists: true}}, {$rename:{oldField:newField}}, false, true);

        // bsonCollection cannot be resolved or is not a field
        //db.bsonCollection.update({oldField: {$exists: true}}, {$rename:{oldField:newField}}, false, true);

        // Wants to add cast to bsonCollection
        //bsonCollection.update({oldField: {$exists: true}}, {$rename:{oldField:newField}}, false, true);

        // Wants to change method to receiver cast
        //((Object) bsonCollection).update({oldField: {$exists: true}}, {$rename:{oldField:newField}}, false, true);

        // Wants to change arguments to one of 8 possible cases
        //bsonCollection.updateMany({oldField: {$exists: true}}, {$rename:{oldField:newField}}, false, true);

        Document filter = new Document(oldField, "{$exists: true}");
        Document update = new Document("$rename", "{"+oldField+":"+newField+"}");
        //Remove arguments to match (bson,bson) or change to updateOne
        //bsonCollection.updateMany(filter.toBsonDocument(), update.toBsonDocument(), false, true);

        bsonCollection.updateMany(filter.toBsonDocument(), update.toBsonDocument());

    }

Runtime Error - The runtime error using collection.updateMany.

2021-06-12 11:49:06 ERROR o.a.c.c.C.[.[.[.[dispatcherServlet].log(175) - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is ongodb.MongoWriteException: Modifiers operate on fields but we found type string instead. For example: {$mod: {<field>: ...}} not {$rename: "{name:firstName}"}] with root cause
com.mongodb.MongoWriteException: Modifiers operate on fields but we found type string instead. For example: {$mod: {<field>: ...}} not {$rename: "{name:firstName}"}
    at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:1017)
    at com.mongodb.client.internal.MongoCollectionImpl.executeUpdate(MongoCollectionImpl.java:993)
    at com.mongodb.client.internal.MongoCollectionImpl.updateMany(MongoCollectionImpl.java:624)
    at com.mongodb.client.internal.MongoCollectionImpl.updateMany(MongoCollectionImpl.java:619)
    at com.myapp.service.CollectionServiceImpl.renameField(CollectionServiceImpl.java:238)
    at com.myapp.service.CollectionServiceImpl.processCollection(CollectionServiceImpl.java:94)
    at com.myapp.service.DatabaseServiceImpl.updateDatabase(DatabaseServiceImpl.java:256)
    at com.myapp.service.DatabaseServiceImpl.processDatabase(DatabaseServiceImpl.java:122)
    at com.myapp.service.DatabaseServiceImpl.processMongoChanges(DatabaseServiceImpl.java:72)
    at com.myapp.controller.DatabaseController.modifyMongo(DatabaseController.java:52)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1063)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1707)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

Build Environment - I am using spring boot 2.4.5, eclipse, Java 8, and Mongodb 4.4. below is the relevant pom file.

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons-lang3.version}</version><!--$NO-MVN-MAN-VER$-->
        </dependency>
    </dependencies>

Frustration - In all the online examples and documentation for executing mongodb commands using java, they all start with db.collection.someMethod(). However, I have yet to find a complete example that shows me that objects make up db.collection.

1 Answer 1

1

Basically, db.collectionName is simplified syntax for db.getCollection("collectionName").

Everything that described as a BSON document in Mongo API reference should be a BSON document (for example, org.bson.Document) in your Java code. In your concrete example with $rename, the documentation says

The $rename operator updates the name of a field and has the following form:

{$rename: { <field1>: <newName1>, <field2>: <newName2>, ... } }

So, the value of $rename is a BSON document. Java implementation should be

Document renameOperator = new Document("$rename", new Document(oldField, newField))

But in your implementation value for $rename is a string, not BSON.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the clarification. I figured it was something simple I was missing. Also, I appreciate the example of how to use the BSON Document.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.