简体   繁体   中英

How do I handle Many-To-One Relationships with Spring Boot + MongoDB

I'm learning how to use MongoDB with spring boot (Spring data MondoDB).

In my current code base that uses PostgreSQL, I have an API GET /api/orders/238392 , which returns a model as follows:

{
    "orderId":238392,
    "status":"FULFILLED",
    "senNumber":"AB8893920",
    "customer":{
        "customerId":"lskj2409023471lskdjflsf",
        "name":"John Doe",
        "email":"john.doe@acme.com",
        "phoneNumber":"7326585306",
        "city":"Los Angeles"
    }
}

I now want to implement this user-case with MongoDB in the backend. This is my model

// Orders.java
@Data
@Document
public class Order {
    @Id
    private Long orderId;
    private String status;
    private String senNumber;
    private Customer customer;
}


//Customer.java
@Data
@Document
public class Customer {
    @Id
    private String customerId;
    private String name;
    private String email;
    private String phoneNumber;
    private String city;
}

My API now works fine. However, I have a problem with the relationship between Orders and Customers. In a relational world, I had the @ManyToOne on customer , as follows:

@ManyToOne
private Customer customer;

I cannot do this in Mongo (that's where I need help).

Now, to my question. I have two collections - Customer and Order . Both of them have CRUD APIs. How do I handle a ManyToOne relationship between Order and Customer.

If a user updates some properties of a Customer (name, email, etc), how does that change reflect in the Orders document?

I have the following options that come to my mind:

  1. When I update a customer, I can update all the order documents with the new customer details (this doesn't seem correct - might cause a lot of performance overhead, i feel)
  2. Using something like @DbRef - if that's the case, can I get some links to related documents or references.
  3. Came across a library called RelMongo that allows you to use @ManyToOne annotations just like how you would in a relational world.

This could be a simple best-practices question, I'm not sure. However, any help goes a long way to improve my understanding!!

Thanks, Sriram

You have the following relationship: one customer can have many orders. For the API that you provided GET /api/orders/238392 , you should reference the customer_id in each order that you have.

If you have a route that has the following property: GET /api/customer/123/orders which will return you all the orders of the customer then you should keep track of the orders of the customer by having an array of reference to the orderId.

Also @ManyToOne and @OneToMany is equivalent to @DBRef .

Here is a good blog post that will help you choose the right schema design for your use case: https://www.mongodb.com/blog/post/6-rules-of-thumb-for-mongodb-schema-design-part-3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM