Would like to learn from you again on Node.js and mongoose.
I have a mongoose schema defined and findOne() returns a doc as below. There are many more elements under the "resource" in the actual doc.
{
"metadata": {"isActive": true, "isDeleted": false },
"test": "123",
"resource": {
"id": "59e94f3f6d5789611ce9926f",
"resourceType": "Patient",
"active": true,
"gender": "male",
"birthDate": "2000-01-01T00:00:00.000Z",
"extension": [
{
"url": "hxxp://example.com/fhir/StructureDefinition/patient-default-bundle",
"valueCodeableConcept": {
"code": "sys",
"display": ""
}
}
],
"link": [],
"careProvider": [],
"communication": [],
"animal": {
"genderStatus": {
"coding": []
},
"breed": {
"coding": []
},
"species": {
"coding": []
}
},
"contact": []
}
}
Question: how can I select all the non-empty fields under 'resource'?
My expected result is as below, i.e all non-empty fields under 'resource' element.
{
"id": "59e94f3f6d5789611ce9926f",
"resourceType": "Patient",
"active": true,
"gender": "male",
"birthDate": "2000-01-01T00:00:00.000Z",
"extension": [
{
"url": "hxxp://example.com/fhir/StructureDefinition/patient-default-bundle",
"valueCodeableConcept": {
"code": "sys",
"display": ""
}
}
]
}
my current coding:
module.exports.findById = function (req, res, next) {
var resourceId = req.params.resourceId;
var resourceType = req.params.resourceType;
var thisModel = require('mongoose').model(resourceType);
console.log("findById is being called by the API [" + resourceType + "][" + resourceId + "]");
thisModel.findOne(
{'resource.id': resourceId, 'metadata.isActive': true, 'metadata.isDeleted': false},
'resource -_id',
function(err, doc) {
if (err) {
globalsvc.sendOperationOutcome(res, resourceId, "Error", "findOne() Not Found", err, 404);
}
else {
if (doc) {
sendJsonResponse(res, 200, doc);
} else {
delete doc._id;
globalsvc.sendOperationOutcome(res, resourceId, "Error", "Id: [" + resourceId + "] Not Found", err, 404);
}
}
}
);
}