简体   繁体   中英

Not Able to use Mongo Db for one to many relation in my ecommerce based microservice

I am newbie to mongo db , I had used mysql in past.

I am creating one ecommerce microservice application which uses java and mongo db .

I want to map(make relationship of one to many) order and product entity in this based on mongo db.

one to many relationship from order to product, how can I make relationship in mongo db.

I am not sure how can we make relationship in mongo db, can some one please guide me with the help of github code or some article ?

may be some code example would be great to see

-Thank you so much

Pretty same as in MySQL. Proper way to implement One-To-Many using MongoDB, is to inject not only ID (foreign key) to an entity, but whole JSON.

For example, when you have Product(id, name):

{
   "id": "6281494ca218170001d2a10b",
   "name": "Meat-fish"
}

And Order(id, username, Product):

{
   "id": "6281494ca218170001d2a112",
   "username": "BigBear",
   "product": {
       "id": "6281494ca218170001d2a10b",
       "name": "Meat-fish"
   }
}

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