0

find를 사용하여 문서 집합에 대해 MongoDB를 이미 쿼리 할 수있는 node.js 모듈을 만들어 결과를 JSON으로 출력했습니다. 내 질문은 node.js가 비동기임을 알기 때문에이 쿼리 (항목)의 결과를 사용하여 MongoDB로 돌아가서 다른 문서 집합을 찾는 쿼리를 만드는 방법이다. 이 쿼리는 기본적으로 해당 직원의 정보가 포함 된 문서 (예 : firstName, lastName 등)를 쿼리하는 데 사용할 수있는 직원 ID 목록을 반환합니다. 그런 다음 결과를 JSON으로 출력하십시오. 첫 번째 쿼리는 기본적으로 특정 사용자가 볼 수있는 모든 직원을 제공합니다. 그런 다음 아래에서 볼 수 있듯이 직원 ID를 가져 와서 개인 정보가 포함 된 다른 문서 집합에 대한 쿼리를 수행해야합니다. 여기 Node.js 및 MongoDB 다른 쿼리 결과 사용

는이 개 문서의 스키마입니다 :

직원

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

사용자 직원이 액세스 할 수 있습니다 (사용자 캐시) 여기

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

내 코드입니다 :

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 
+0

제안 - 직원 ID를 기반으로 문서 집합을 검색하는 데 '$ in'을 사용하고 몽구스를 보면 조작 유형이 간단 해집니다. – WiredPrairie

답변

2

당신이하고 싶은 것은 가능하지만 아마도 Mongo를 가장 효과적으로 사용하지는 못합니다. 필자는 데이터가 실제로 어떻게 사용될 것인지에 대한 몽고 문서를 디자인하는 경향이있다. 따라서 내가 볼 수있는 사용자 목록에 사용자의 이름이 나타나기를 원한다면 필요한 모든 정보를 얻기 위해 여러 번 왕복 여행을 할 필요가 없도록 해당 데이터를 임베드합니다.

{ 
    "_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" 
    } 
} 

예, 중복되는 데이터를하지만 당신은 극적으로 데이터베이스에 왕복의 수를 줄일 수있어 : 나는 다음과 같은 일을 할 것입니다. 이는 일반적으로 테이블을 조인 할 수 없으므로 문서 기반 데이터베이스를 사용할 때 만들어지는 단점입니다.

+1

나는 당신의 충고를 받아 내 스키마를 개작했습니다. 나는 이제 내가 필요한 것을 얻기 위해 하나의 질의를 할 수있다. – FarscapePROJ

관련 문제