Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

357
Views
Rename a field in Mongodb collection using Spring data and Java

I have a collection with several fields and would like to update both the field name through Java program. For example

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

Now I have a requirement to rename the field1 to say field11. How do I do it using Spring Data.

Basically this ops using Java https://docs.mongodb.com/manual/reference/operator/update/rename/

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

@Autowired
MongoTemplate mongoTemplate;

void renameFunction() {
    BasicDBObject searchQuery = new BasicDBObject();
    BasicDBObject updateQuery = new BasicDBObject();
    updateQuery.append("$rename",new BasicDBObject().append("field1", "field11").append("field2", "field22"));  
 mongoTemplate.getCollection("Collection_Name").updateMany(searchQuery,updateQuery);
         }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!