2016-11-15 4 views
-1

안녕하세요. mongodb에 익숙합니다. node.js를 사용하는 취미 프로젝트 중 하나를 구현하고 있습니다. 아래에서 수행 할 스키마 및 쿼리를 도와주세요. 계획.mongoose 및 node.js에 대한 참조 문서 스키마 만들기

저는 처음 방문했을 때 각 환자의 세부 정보가 등록 된 병원을 가지고 있으며, 방문 할 때마다 해당 환자의 방문을 참조로 추가해야합니다.

var visitschema = mongoose.schema({ 
            objectid:"1234", 
            patientid:"12345678" 
            visitdate:date, 
            reason:string, 
            consultingdoctor:string 
            consultingfee:number      
            }); 

나에게 적절한 스키마를 제공하시기 바랍니다과 쿼리가 Node.js를 응용 프로그램에서이를 달성하기 위해 같은

var patientschema=mongoose.schema(
        { 
         objectid:"12345678" 
         patientname: string, 
         patientDOB:Date, 
         Gender:string, 
         registrationfee:number, 
         visits:[{objectid:1234},{objectid:5678}] 
        }); 

및 방문 스키마입니다.

+0

refrences에 실제 값을 쓰지 않습니다. ref 필드를 전달해야합니다. 이 링크를 확인하십시오. http://mongoosejs.com/docs/populate.html – anwerj

+0

예제 또는 샘플 응용 프로그램이 있습니까? – user2159740

답변

0

두 가지 스키마는 다음과 같아야합니다.

var patientschema=mongoose.schema(
{ 
    patientname: string, 
    patientDOB:Date, 
    Gender:string, 
    registrationfee:number, 
    visits:[{ type: mongoose.Schema.Types.ObjectId, ref: 'Visit' }] 
}); 

var visitschema = mongoose.schema(
{ 
    patientid:{ type: mongoose.Schema.Types.ObjectId, ref: 'Patient' } 
    visitdate:date, 
    reason:string, 
    consultingdoctor:string 
    consultingfee:number      
}); 

var Visit = mongoose.model('Visit', visitschema); 
var Patient = mongoose.model('Patient', patientschema); 

이제 우리는 새로운 환자 엔트리를 만들 수 :

var p1 = new Patient({name: 'Some Name', patientDOB : '2016-11-11', Gender : 'male', registrationfee : 123}); 

지금이 환자에게 방문을 추가 할 수 있습니다. 이

v1.save(function(err, res){ 

}); 

몽구스는 내부적으로 환자 방문 어레이에 V1의 ID를 추가합니다 흥미로워 곳

var v1 = new Visit({patientid : p1._id, ... Fill Other Fields here }); 

지금이 방문 저장, 그게 전부 때. 나중에 검색 할 수 있습니다.

Patient.find().populate('visits').exec(function(err, res){ 
    // Here the P1 with have array of Visits 
}); 
관련 문제