1

I have made a mistake naming subcollection in MongoDB, unfortunately I named them using hyphen :/

Here is sample :

{
id: "..."

"A-Section": {
         "val":1 
       }
}

I need to access the "val" field. Unfortunately hyphens seems to block MongoDB.

So I have to option :

  • Find a trick to access the "A-Section"
  • Rename all the "A-Section"

In both case I do not know how to do it and after few researches, I only found answer if the collection name contains hyphen but not a subcollection.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Jean G
  • 153
  • 1
  • 1
  • 7

1 Answers1

2

The database contains collections of documents, each document has it's own key-value pairs.

I assume you ment renaming a field in the collection and not an array inside a field.

So you can use the $rename operator to rename all fields in the collection

ex:

db.collectionName.update( {"A-Section": {$exists:true}}, {$rename: {"A-Section": 'ASection'} }, {multi: true} )
Omri Luzon
  • 3,975
  • 6
  • 20
  • 29