0

I have created a node.js module that can already query MongoDB for a set of documents using a find and output those results to JSON. My question is, knowing that node.js is asynchronous, how can I use the results from this query (items) to create a query that goes back to MongoDB to find another set of documents. This query basically returns a list of employee ids that can be used to query documents containing information on those employees(i.e. firstName, lastName etc.). Then output those results instead as JSON. The first query is basically saying, give me all of the employees that can be viewed by a particular user. I then need to take the employee ids and do a query on another set of documents that contains those individuals information, like you see below.

Here are the two documents schema:

Employee

{
"_id" : ObjectId("5208db78ecc00915e0900699"),
"clientId" : 1,
"employeeId" : "12345",
"lastName" : "DOE",
"firstName" : "JOHN",
"middleName" : "A",
"badge" : "8675309",
"birthDate" : "10/12/1978"
}

Users an employee can access (User Cache)

{
"_id" : ObjectId("520920a99bc417b7c5e36abf"),
"clientSystem" : "SystemX",
"customerNumber" : "1",
"clientUserId" : "jdoe3",
"securityCode" : "authorize",
"employeeId" : "12345",
"creationDate" : "2013-Aug-12 13:51:37"
}

Here is my code:

exports.employeeList = function(req, res) {
    console.log(req.params);
    var clientSystem = req.query["clientSystem"];
    var clientUserId = req.query["clientUserId"];
    var customerNumber = req.query["customerNumber"];
    var securityCode = req.query["securityCode"];

 if (clientSystem != null && clientUserId != null && customerNumber != null && securityCode != null){
    db.collection('ExtEmployeeList', function(err, collection){
        collection.find({'clientSystem': clientSystem, 'clientUserId':clientUserId, 'customerNumber':customerNumber, 'securityCode': securityCode}).toArray(function (err, items){
             console.log(items);
             res.jsonp(items);
        });//close find
     });//close collection
}//close if
else {
   res.send(400);
}//close else
};//close function
1
  • 2 suggestions -- use $in to retrieve a set of documents based on the employee ID, and look at Mongoose, as it makes that type of operation simple. Commented Aug 12, 2013 at 21:50

1 Answer 1

2

What you're wanting to do is possible, but probably not the most effective use of Mongo. I tend to design Mongo documents around how the data will actually be used. So if I needed the user's names to show up in a list of users I can view, I would embed that data so I don't have to do multiple round trips to mongo to get all the information I need. I would do something like the following:

{
  "_id" : ObjectId("520920a99bc417b7c5e36abf"),
  "clientSystem" : "SystemX",
  "customerNumber" : "1",
  "clientUserId" : "jdoe3",
  "securityCode" : "authorize",
  "employeeId" : "12345",
  "creationDate" : "2013-Aug-12 13:51:37"
  "employee": {
    "_id" : ObjectId("5208db78ecc00915e0900699"),
    "clientId" : 1,
    "employeeId" : "12345",
    "lastName" : "DOE",
    "firstName" : "JOHN",
    "middleName" : "A",
    "badge" : "8675309",
    "birthDate" : "10/12/1978"
  }
}

Yes, you are duplicating data but you're dramatically reducing the number of round trips to the database. This is typically the tradeoff you make when using document based databases since you can't join tables.

Sign up to request clarification or add additional context in comments.

1 Comment

I took your advise and revisted my schema. I now can make one query to get what I need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.