如何在MongoDB中建立外键关系?

huangapple go评论73阅读模式
英文:

How to establish a foreign key relationship in MongoDB?

问题

我有一个名为customerproductpurchase_order的PostgreSQL数据库,每个表都有id_作为主键。表purchase_order还有customer_idcustomer表的id_字段)和product_idproduct表的id_字段)作为外键。

我想在MongoDB上创建相同的数据库。然而,我不确定如何建立外键。我已经进行了研究,发现这在很大程度上取决于我如何做。所以,这是我在MongoDB上创建purchase_order表的方式:

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

我的目标是编写一个Spring应用程序来执行CRUD方法。在PostgreSQL上,我在数据库上添加了外键约束,并在我的代码上添加了@OneToMany@ManyToOne注解来建立外键关系。然而,我不确定如何在MongoDB上实现它。我的MongoDB文档是否正确表示了外键?如何确保在我的Spring应用程序中为MongoDB建立外键关系?

英文:

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?

答案1

得分: 5

如果您在后端使用JavaScript,您想要的功能可以很容易地通过Mongoose实现!

这涉及为实体创建模式(Schema),并为实体的外键字段/列指定ref属性,该属性与现有模型的名称匹配。

随后,每当您查询数据库以获取单个文档时,还可以添加或填充外键字段的详细信息。

如果您尚未使用或熟悉Mongoose,请在开始使用mongoose中查看,然后继续查看这份文档,其中为您在查询时如何填充外键字段提供了示例。

在您的特定案例中,给定的模式可能如下所示:

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

const CustomerSchema = new Schema({
   _id: {
    type: Schema.Types.ObjectId,
  }
  // 其他相关字段
})

const ProductSchema = new Schema({
   _id: {
    type: Schema.Types.ObjectId,
  }
  // 其他相关字段
})

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'
  }
});

// 您的模型
const Customer = mongoose.model('Customer', CustomerSchema);
const Product = mongoose.model('Product', ProductSchema);
const ProductOrder = mongoose.model('ProductOrder', ProductOrderSchema);


希望您可以相对轻松地解决这个问题!

英文:

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!

huangapple
  • 本文由 发表于 2020年9月9日 14:32:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63805955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定