Java code to rename a collection field


Is there a way to perform the below activity using Java

{_id : ObjectId(xxxxxxxxxxxx), field1: "value1", field2  : "value2"}

For example my existing collection has field structure as above and my requirement is to rename field1 to say field11.

I am primarily looking at Spring Data lib but not finding a way. Wondering if there is another library/framework using which I can achieve the same through Java code ?

Hi @ragarwal1_N_A, hope you are doing well. As I am not very familiar with the Java Driver, here is an idea of how to go about renaming a field in a document:

    BasicDBObject updateDoc = new BasicDBObject();
    BasicDBObject updates = new BasicDBObject().append("field1", "field11").append("field2", "field22")
    updateDoc.append("$rename", updates);  

and then using the above updateDoc inside an updateMany command along with the searchQuery as the first parameter will do the job.

I hope this helps, please feel free to reach out if you have any doubts.

Thanks. Regards.
Sourabh Bagrecha,
Curriculum Services Engineer.