2016-11-20 85 views
2

學習FHIR並嘗試使用使用MongoDb作爲數據庫的MEAN堆棧實現,我想在我的問題上尋求您的幫助。Mongodb for HL7-FHIR

當我得到一個新的資源文檔的POST請求時,我會將它插入到MongoDB中。由於MongoDB會將_id(對象標識)作爲唯一標識添加到資源中。當我檢索文檔時,它會有額外的字段_id。我認爲它會使資源不再符合規定,因爲_id沒有在資源中定義。

我可以知道如何處理這個問題嗎?這個額外的_id是否在FHIR資源中?

最好的問候, 自動運行

回答

0

肯定的,_id不會是符合的。你不能把它改爲'id'?

+0

感謝Grahame。感恩節快樂。 Mongo使用_id。此外,它位於頂層,但不在標識符子組件下。 – Autorun

+0

所以它是同樣的事情,因爲我們所說的「ID」,但用不同的名稱? –

+1

嗨格雷厄姆,再次感謝! – Autorun

0

也許你可以看看Spark server,它也使用MongoDB來存儲資源。在Spark.Store.Mongo命名空間中,你會看到一些輔助的方法來一個蒙戈BSONdocument轉換爲FHIR資源。

+0

謝謝Mirjam。我會研究它,但我不太瞭解C#。 – Autorun

+0

好了,沒問題,我希望你能看到我們是如何採取從MongoDB的該文件,並採取了我們使用數據的其餘部分之前已經蒙哥添加額外的領域。 –

1

所以,我還使用MongoDB - 連同貓鼬 - 在nodejs中實現FHIR。 我剛剛加入了一個名爲id字段在架構定義貓鼬這樣

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; 

,然後_id字段從id取其值upserting一個創建時/更新資源

我的代碼患者資源

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

感謝Shalkam的回答。 – Autorun