Skip to main content

Foreign Keys

Introduction

MongoDB is not a relational database system, so there is no native concept of foreign keys or relationships between collections. The API and all Weightron.Cloud applications implement foreign keys at a high level, without the database itself doing anything non-conventionally.

During an update or insert operation, the target collection will be checked for foreign keys. For each one, the foreign collection will be checked using the properties given and the data uploaded. If a match is found, the foreign object is loaded into the importProperty (from the foreign key definition) of the local document. At the end of the request, the local document will be fully populated with the foreign documents it references, which is inserted into the collection.

Foreign key example

info

The importProperty may be the same as the property key.

This mechanism enables the API to filter and project on the full range of data, including the foreign properties. This is also possible with MongoDB's native $lookup aggregation step, but with a major performance loss.

Foreign keys are defined by your system developer and cannot be adjusted via the API.

Update/Delete Rules

Foreign key update and delete rules are implemented at the application level, and handled by the API automatically. When a document is changed, any documents that reference it (through a foreign key) may be updated automatically depending on the rule set in the key. Your authorized reseller can give information on the rules assigned to the application.

RuleDescription
IgnoreNo action will be taken. Referencing documents will keep the previous document version.
CascadeChanges to the root document will be automatically mirrored onto referencing documents.
RestrictThe original action will be blocked if any references exist.
NullifyReferences will be set to null, and the original action will continue normally.

API Requests

The documents in the database are stored with foreign documents included. This may not be required for every request, so the query parameter foreignkeys is available to decide whether or not the imported data is included.

When foreignkeys is false, the returned documents will not contain the referenced documents. Only the properties of that collection will be included, so the resulting object should be compatible with that collection's schema.

When true, the documents will be returned as-is from the database.