I have a schema to build, it is a bus station application, which stores the distance or other info between two nearby bus stations, it is unlikely to store one station id for the index or unique key, I think a better to do it is to group station 1 and station 2 as unique and index key, but I am a bit not confident whether it is the right way to do it, put 2 bus stations id into a array, make this array as index and unique key?
1 Answers
That sounds very reasonable... It's a "relationship table" or a many-to-many join table with additional attributes. You would store the distance as an attribute of the M:N relationship./ the two bus station ids would form the composite primary key.
Look at the image in section 2.1.5 of this modeling guide
You may want to learn a little bit about database design techniques. If so, some useful sources on databases and modeling are:
- Fundamentals of Database Systems by Elmasri and Nevathi - Very technical about all aspects of database and covers modeling in depth
- An Introduction to Database Systems by CJ Date - similar to above
- A Tutorial on Normal Form (BCNF) - BCNF prescribes a means of bringing out your data model by iteratively applying a rule to your model till it meets normal form (is efficient - barring intended redundancy).
- Wikipedia entry looks pretty concise - as above re BCNF but looks nice and concise (perhaps focus on sections 3 and 4)
EDIT: Update relevant to Mongo DB
Actually, the above is all pretty general for database modeling. Having a read of some of the excellent resources on Data Modeling Considerations for MongoDB Applications I think you need more specific guidance.
As such, I refer you to this informative SO post: how-to-organise-a-many-to-many-relationship-in-mongodb. The author gives a good explanation that sounds like what you're after. There's even references to docs and a video.

- 1
- 1

- 5,995
- 2
- 27
- 40