简体   繁体   English

Mongodb更改文档字段名称

[英]Mongodb change Document field name

I have an application that reads a JSON String(not generated by me) that contains a field with a '.' 我有一个应用程序读取一个JSON字符串(不是由我生成的),该字符串包含带有“。”的字段。 and converts it to BSON as such: 并将其按以下方式转换为BSON:

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

Calling collection.insertOne() is causing me problems as mentioned in What characters are NOT allowed in MongoDB field names? 调用collection.insertOne()导致我遇到问题,如MongoDB字段名称中不允许使用哪些字符? .

I'm now trying to change the field names within document that contain periods. 我现在正在尝试更改包含句点的文档中的字段名称。 Is there a way to iterate through the document and change the field names? 有没有办法遍历文档并更改字段名称?

I would point you towards db.collection.update(), something like: 我将把您指向db.collection.update(),类似:

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

By setting multi:true you will update all the documents that match the query criteria. 通过设置multi:true,您将更新所有符合查询条件的文档。

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

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

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