1

I have 2 collections, Country and cities, cities should hold the ObjectId reference of Countries,

On Creation of new city,

i want cities Collection should Look like

{
    "_id" : ObjectId("5c83505e5609a7274855ae4c"),
    "city_id" : "13456",
    "city_name" : "CapeTown",
    "country" :ObjectId("5c8350235609a7274855ae4b")
    "_class" : "com.mycompany.myapp.domain.cities"
}

I don't want something like

{
    "_id" : ObjectId("5c83505e5609a7274855ae4c"),
     "city_id" : "13456",
    "city_name" : "CapeTown",
    "country" : {
        "$ref" : "countries",
        "$id" : ObjectId("5c8350235609a7274855ae4b")
    },
    "_class" : "com.mycompany.myapp.domain.cities"
}

How to write the model class to achieve my requirement?

  • From a coding perspective you have to manage it manually, the City class should have a field called country of type ObjectId. When you create a new City, you must take care of retrieve the country and set the according value to the field inside the City object. The question is: what does it mean exactly "but i need reference directly"? What is your expectation doing so? – verodigiorgio Mar 23 '19 at 17:16

0 Answers0