简体   繁体   English

使用 Spring 数据和 Java 重命名 Mongodb 集合中的字段

[英]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.我有一个包含多个字段的集合,并且想通过 Java 程序更新两个字段名称。 For example例如

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

Now I have a requirement to rename the field1 to say field11.现在我需要将 field1 重命名为 field11。 How do I do it using Spring Data.我如何使用 Spring 数据来做到这一点。

Basically this ops using Java https://docs.mongodb.com/manual/reference/operator/update/rename/基本上这个操作使用 Java https://docs.mongodb.com/manual/reference/operator/update/rename/

@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);
         }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM