学习 FHIR 并尝试使用使用 MongoDb 作为数据库的 MEAN 堆栈实现,我想就我的问题寻求您的帮助。
当我收到对新资源文档的 POST 请求时,我会将其插入到 MongoDB 中。由于 MongoDB 会将 _id(对象 ID)作为唯一 ID 添加到资源中。当我检索文档时,它会有额外的字段 _id。我认为这将使资源不再合规,因为资源中未定义 _id。
我可以知道如何处理这个问题吗?这个额外的 _id 在 FHIR 资源中是否重要?
最好的问候, 自动运行
最佳答案
因此,我还使用 MongoDB 以及 mongoose 在 nodejs 中实现 FHIR。 我刚刚在 mongoose 的模式定义中添加了一个名为 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
中获取其值
我用于更新患者资源的代码
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;
}
关于用于 HL7-FHIR 的 MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40701191/