1

I need to insert a field in existing collection in mongodb at particular order. For e.g. If I have collection which contains document as:

item:{ 
       //mandatory field, will exist in each document
       id: "1", 
       //non-mandatory fields, may or may not exist in each document 
       name: "john", 
       age: "22", 
       email:"john@gmail.com" , 
  }

Now I want to insert a field "gender" after age and before email so that my document becomes:

item:{
       id: "1",
       name: "john",
       age: "22",
       gender: "male",
       email:"john@gmail.com"

}

My collections is basically of a POJO generated from xsd, A document may or may not contain certain fields. Therefore I can't just override the json.

amit28
  • 176
  • 8
  • 1
    What's the point in having keys in order? – dikesh Feb 08 '16 at 14:41
  • I have to generate xml using this data in proper order, if keys are not in proper order then xml will fail validation against the xsd. – amit28 Feb 08 '16 at 14:46
  • Possible duplicate of http://stackoverflow.com/questions/5046835/mongodb-field-order-and-document-position-change-after-update – JohnnyHK Feb 08 '16 at 14:48
  • @JohnnyHK I am not updating existing field, i am inserting new fields. Also I did'nt find any solution in that post. – amit28 Feb 08 '16 at 14:50
  • 1
    It's still the same issue. There isn't any way to control the order of the added field. You're probably best off tweaking the field order as you generate your xml. – JohnnyHK Feb 08 '16 at 14:53

0 Answers0