Referencing collections in mongodb

1

I have a model of an airport in mysql and I plan to upload it to the mongodb via links between collections. In the following images are examples of documents from my collections. (airplane, airport and flight)

The id in the flight is the id_voo, in the airplane is the tailnum and in the airport it is idAirport. In mysql the relations are from 1: M from airplane to flight and from 1: M from flight to airport. I have exported this data with the foreign keys and I want to reference it between documents in mongodb. The mongo db immediately assigned an objectid as main and that was not what he intended. How can I set this?

    
asked by anonymous 27.09.2015 / 17:31

1 answer

2

As you know, MongoDB is not relational, so there is no way to define a native relationship between two documents. You have two alternatives most common in Mongo:

  • Embedded (nested) documents
  • Manual reference, using _id
  • I recommend you read a little about this topic because it's kind of long to explain here on the site. Each has its advantages and disadvantages.

    Regarding the primary key, in MongoDB this is always the _id attribute and is immutable. You have to either use _id in your programs, putting yourself _id at the time of insertion, otherwise Mongo will generate one for you. The _id does not have to be a simple value, it can be a sub-document too, like "_id": {"numero": 123, "apartamento": "25-B"} .

        
    27.09.2015 / 18:32