0

As we already known as we don't have table joins in Mongodb but if we want to get result from 2 different documents than how we can query to Mongodb? Consider following example.

Document 1 - > department

{Id_:123,name:technical,location:"B Wing"} {Id_:234,name:account,location:"main Wing"} {Id_:547,name:HR,location"C Wing"}

Document 2 - > employee

{Id_:a101,name:Peter,dept_id:234,DOB:2010-01-01} {Id_:a102,name:Liomo,dept_id:547,DOB:1950-01-01} {Id_:a103,name:Juno,dept_id:123,DOB:1990-01-01} {Id_:a104,name:ole,dept_id:554,DOB:2011-01-01}

So how can we get all fields like (EmployeeName, DepatmentName, DOB) in one result, I am not getting any way please help me out

Thanks in advance

Soma
  • 861
  • 2
  • 17
  • 32
nish71
  • 275
  • 1
  • 7
  • There is a [way to do joins in MongoDB](https://stackoverflow.com/questions/2350495/how-do-i-perform-the-sql-join-equivalent-in-mongodb) as of version 3.2. – str Dec 05 '17 at 13:09

1 Answers1

0

There is no way. Mongodb does not support joins. The NoSQL way is to denormalize your data, meaning you embed a copy in A of the fields from B you need in A.

There is such a thing as database references, but all that does is provide syntactic sugar for combining the result of two queries client side.

By the way, if your data is really relational in nature (like an employee database the way I would probably design it), perhaps a relational database would be a better fit.

Mzzl
  • 3,926
  • 28
  • 39
  • yes but is there a way with in query suppose we dont need reslt form 2 tables but display only those record which other document have it like in SQL select * from employee where dept_id in (select _id from department). there must have way... – nish71 Dec 30 '13 at 11:09
  • I'm sorry, but there is no way. You will have to do two queries. I looked in the docs to see if you could perhaps do it with a map reduce function, but neither map nor reduce is allowed to access the database. If you need this result a lot, the mongodb way would be to embed a copy of the Department Name in the Employee collection. How often does the name of a department change anyway? You chose a NoSQL database. This is the NoSQL way of working. – Mzzl Dec 31 '13 at 09:13