How to get data from two collections in spring using Mongotemplate or MongoRepository

坚强是说给别人听的谎言 提交于 2019-12-25 03:15:53

问题


I have started working with spring and mongodb few months ago. Till now I din't get how to fetch data from multiple collection using Mongotemplate or MongoRepository. I have two collections Person and Contacts.now I want to fetch list of Customer along with Contacts. Customer is having the is is _id and Contact is having the relation id is customerId So how can i get the customer contact details of the data.


回答1:


Your data needs de-normalization, think the MongoDB way. You need to store 'Person/Customer' data along-with corresponding 'Contacts'. This is a 1:n kind of association. You can easily store required data in the following schema , below is a sample 'Person/Customer' document which embeds his 'Contact' details=>

{
 name:"abc",
 age: 35,
 Contact:{[email:"abc1@gmail.com",mobile:123],[email:"abc2@gmail.com",mobile:234]}
}

If you end up normalizing data like mentioned, you tend to throw away the powerful embedding capability that MongoDB offers and end up doing joins in code.



来源:https://stackoverflow.com/questions/26865047/how-to-get-data-from-two-collections-in-spring-using-mongotemplate-or-mongorepos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!