Mongodb更改文档字段名称

时间:2015-07-16 16:03:54

标签: java mongodb mongodb-java

我有一个应用程序读取一个JSON字符串(不是我生成的),其中包含一个带有'的字段。并将其转换为BSON:

Document document = new Document(Document.parse(jsonString));

调用collection.insertOne()会导致What characters are NOT allowed in MongoDB field names?中提到的问题。

我现在正在尝试更改包含句点的文档中的字段名称。有没有办法迭代文档并更改字段名称?

1 个答案:

答案 0 :(得分:0)

我会指向db.collection.update(),类似于:

db.collection.update({*your_find_query*},{$rename:{*fields_to_rename*}},{multi:true})

通过设置 multi:true ,您将更新符合查询条件的所有文档。

更多信息:http://docs.mongodb.org/manual/reference/method/db.collection.update/