2

Learning FHIR and trying to implement with MEAN stack which uses MongoDb as database, I would like to seek your help on my question.

When I get the POST request for a new resource docment, I will insert it into MongoDB. Since the MongoDB will add the _id (object id) to the resources as a unique id. When I retrieve the document, it will have the extra field _id. I think it will make the resources not compliance any more since the _id is not defined in the resources.

May I know how to handle this issue? Will this extra _id matter in the FHIR resource?

Best regards, Autorun

Autorun
  • 319
  • 2
  • 8
  • 20

3 Answers3

1

So, I'm also using MongoDB - along with mongoose - to implement FHIR in nodejs. I've just added a field called id in the schema definition for mongoose like this

import mongoose from 'mongoose';
import shortid from 'shortid';
class resource extends mongoose.Schema {
  constructor(schema) {
    super();
    this.add({
      // just added this to make MongoDB use shortid
      _id: { type: String, default: shortid.generate },
      id: { type: {} },
      id_: { type: {} },
      implicitRules: { type: String },
      implicitRules_: { type: {} },
      language: { type: String },
      language_: { type: {} },
      ...schema
    });
  }
}
export default resource;

and then _id field takes its value from the id when create/update a resource

my code for upserting a patient resource

upsert(root, params, context, ast) {
    const projection = this.getProjection(ast);
    if (!params.data.id) {
      params.data.id = shortid.generate();
    }
    params.data.resourceType = 'Patient';
    const upserted = model
      .findByIdAndUpdate(params.data.id, params.data, {
        new: true,
        upsert: true,
        select: projection
      })
      .exec();

    if (!upserted) {
      throw new Error('Error upserting');
    }
    return upserted;
  }
Shalkam
  • 733
  • 6
  • 12
0

yes, the _id will not be conformant. You can't change it to 'id'?

Grahame Grieve
  • 3,538
  • 3
  • 15
  • 17
  • Thanks Grahame. Happy Thanks Giving. Mongo uses _id. Moreover, it is on the top level, but not under the identifier subcomponent. – Autorun Nov 25 '16 at 20:06
  • so it's the same thing as we call "id" but with a different name? – Grahame Grieve Dec 04 '16 at 05:24
  • Yes, it is similar between id and _id. I am brainstorming to put a resource such as Patient resource as only part of the Mongo document. A document contains 3 main parts. 1. _id which is resource id, not business patient id, 2. metadata which keep track of history or necessary resource qualifiers, 3. the actual resources. The identifier array in the Patient resource will contain both resource id (_id) and patient MRN. However, by doing this, a single 'create' operation, will be in 2 operations - add, then update with the new resource id under 'identifier' – Autorun Dec 10 '16 at 21:15
  • My dump idea for storing a single resource in Mongo - { _id: Object_id, _history: history_record, _metadata: other_metadata, fhir_resource: fhir_resource }. Within the FHIR resource, according to the specification, I will have an array of identifier which can be driver license, medical record number as well as Mongo resource id, i.e. _id. – Autorun Dec 10 '16 at 21:19
0

Perhaps you can take a look at the Spark server, which also uses a MongoDB to store the resources. In the Spark.Store.Mongo namespace you will see some helper methods to convert a Mongo BSONdocument to a FHIR resource.

Mirjam Baltus
  • 2,035
  • 9
  • 13
  • Thanks Mirjam. I will look into it but I don't know much about C#. – Autorun Nov 25 '16 at 20:06
  • Okay, no problem, I hope you can see how we take that document from the MongoDB and take out the extra fields that Mongo has added before we use the rest of the data. – Mirjam Baltus Dec 05 '16 at 16:14