4

I have a postgreSQL database that has 3 tables named customer, product and purchase_order. Every table has id_ as the primary key. Table purchase_order also has customer_id (id_ field of customer table) and product_id (id_ field of product table) as foreign keys.

I want to create the same DB on MongoDB. However, I'm not sure how to establish foreign keys. I have researched it and found out that it is pretty much up to me how to do it. So, this is how I did purchase_order table on MongoDB:

{"_id":{"$oid":"5f5639cdb675b2a13db059b3"},
"customer_id":{"$oid":"5f563a08b675b2a13db059b1"},
"product_id":{"$oid":"5f563a13b675b2a13db059b2"},
"status":false}

My goal is to write a spring application to perform CRUD methods. On postgreSQL, I added foreign key constraints on DB and @OneToMany & @ManyToOne annotations on my code to establish foreign keys. However, I'm not sure how to do it for MongoDB. Does my MongoDB document correctly represent the foreign keys? How can I make sure to establish my foreign keys on my spring application for MongoDB?

Dilara Daria
  • 63
  • 1
  • 7
  • MongoDB has no concept of foreign key constraint, like in a relational database. Here is general documentation on [Data Models](https://docs.mongodb.com/v4.2/data-modeling/index.html) for MongoDB. Data can have entities with relationships (1:N, N:N, etc.), but these are modeled for MongoDB documents and flexible schema. Also see: [SQL to MongoDB Chart](https://docs.mongodb.com/v4.2/reference/sql-comparison/index.html). – prasad_ Sep 09 '20 at 09:05

1 Answers1

5

If you are using JavaScript on your backend, what you are looking for can be easily achieved with Mongoose!

This involves creating a Schema for an entity and specifying a ref property for the entity's foreign key field/column that matches the name of an existing model.

Following this, whenever you query the database for a single document, you can also add or populate the details for the foreign key field.

Kindly view this on getting started with mongoose if you are not using it or familiar with it, and then move on to this documentation which provides examples for populating the foreign key field when you make a query.

In your specific case a given schema could look like this:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const CustomerSchema = new Schema({
   _id: {
    type: Schema.Types.ObjectId,
  }
  // Other relevant fields
})

const ProductSchema = new Schema({
   _id: {
    type: Schema.Types.ObjectId,
  }
  // Other relevant fields
})

const ProductOrderSchema = new Schema({
  _id: {
    type: Schema.Types.ObjectId,
  },
  customer_id: {
    type: Schema.Types.ObjectId,
    ref: 'Customer'
  },
  product_id: {
    type: Schema.Types.ObjectId,
    ref: 'Product'
  }
});

// Your models
const Customer = mongoose.model('Customer', CustomerSchema);
const Product = mongoose.model('Product', ProductSchema);
const ProductOrder = mongoose.model('ProductOrder', ProductOrderSchema);


Hope you can get this sorted out relatively easily!

rexess
  • 729
  • 4
  • 7
  • Thank you for your answer. I'm sorry if miss the point but I use JSON format for my streams. Would it still be possible to use mongoose as you suggested? – Dilara Daria Sep 09 '20 at 07:08
  • What language are you using? JavaScript? If yes, mongoose should fit in easy. – rexess Sep 10 '20 at 05:19
  • @rexessilfie when to use nested docs and when to use foregin key? – Krishnadas PC Feb 21 '23 at 08:03
  • That is a great question with many nuances to consider! This post discusses embedding (for example with sub-documents) vs a separate document (with a ref): https://stackoverflow.com/questions/5373198/mongodb-relationships-embed-or-reference – rexess Feb 21 '23 at 17:19