3

Does anyone know how to achieve foreign key like behaviour in a Meteor (javascript web framework)?

I think MongoDB works differently than sqlite3 or MySQL. I also read somewhere that there's a way to achieve this other than using foreign keys..

jdscosta91
  • 706
  • 1
  • 8
  • 16

2 Answers2

4

MongoDB is a document store, not a relational database. As such there is no concept of foreign keys with features like cascading updates. However, you can still reference one document from within another document by its _id (which is like the primary key). So you could have a User collection with documents like this:

{
  _id: "myId",
  name: "Rahul",
  locationId: "some_location_id"
}

If you wanted to know more about the location, you could search the Location collection for a document with an _id equal to the locationId you stored on the User document.

See Foreign keys in mongo for more on how to approach this.

Community
  • 1
  • 1
Rahul
  • 12,181
  • 5
  • 43
  • 64
1

As a non-relational database, Mongo does not support joins like a traditional relational database. As a document database however, Mongo allows document nesting which may very well achieve what you want without traditional joins.

That said, the Meteor core group does have a plan to easily allow joins in subscriptions, as well as support for traditional relational databases.

TimDog
  • 8,758
  • 5
  • 41
  • 50