Harish Veeravalli
Harish Veeravalli

Reputation: 9

Mongo DB querying from two different Schema(Services)

I have two Schemas running in my localhost via Node.Js. Let say the two services are Employee and Employee Salary.

  localhost/emp
  var EmpSchema = mongoose.Schema({

     Emp_ID : Number,
     Emp_Email_ID : String,
     Emp_First_Name : String,
     Emp_Last_Name : String,

   });

   localhost/empsalary

      var EmpSalarySchema = mongoose.Schema({

      Emp_ID : Number,
      Emp_Monthly_Salary : Number,
      Emp_Annual_Salary : Number
     Emp_Loan_Detail : String,

     });

How do I need to query in get method such that I get Emp_Monthly_Salary and Emp_First_Name

Upvotes: 0

Views: 974

Answers (1)

Reto
Reto

Reputation: 3141

MongoDb is a NoSQL Database and one of its limitations is that it not allows making JOINs between collections directly in one query.

The typical ways to get around this limitiation:

  • query one collection and use the mongoose "populate" feature to add data from other collections to the result set: http://mongoosejs.com/docs/populate.html (probably the preferred solution for your issue)

  • remodel your data so it fits better to the typical queries you have (and often: allow some redundancy), so if you always query firstname and salary together you might want to store them together using a subdocument: http://mongoosejs.com/docs/subdocs.html

  • just use two queries and do the JOINing manually in node.js code

  • if your data and your queries are really requiring lots of JOINs (are very relational), mongo might not be the right choice for your problem (http://docs.mongodb.org/manual/faq/fundamentals/)

Upvotes: 3

Related Questions