MongoDB - 查询作为数组的内部对象
MongoDB - Querying inner object that is an array
我是 MongoDB 查询的新手,正在寻找有关如何检索符合搜索条件的文档的一些指导。特别是,我需要找到代码为 JPID 的文档的所有 id(s),即 "code": "JPID" .我们可以假设集合的名称是 systems.
因为这是一个嵌套对象,所以我想使用 $unwid - 但我仍然不知道如何去做。这个例子非常简单。感谢任何帮助和指导。
{
"resourceType": "NamingSystem",
"id": "example-id",
"name": "Austalian Healthcare Identifier - Individual",
"status": "active",
"kind": "identifier",
"date": "2015-08-31",
"publisher": "HL7 Australia on behalf of NEHTA",
"responsible": "HI Service Operator / NEHTA",
"type": {
"coding": [
{
"system": "http://hl7.org/fhir/v2/0203",
"code": "JPID";
"display": "National unique individual identifier"
}
],
"text": "IHI"
},
"description": "Australian HI Identifier as established by relevant regulations etc",
"uniqueId": [
{
"type": "oid",
"value": "1.2.36.1.2001.1003.0",
"comment": "This value is used in Australian CDA documents"
},
{
"type": "uri",
"value": "http://ns.electronichealth.net.au/id/hi/ihi/1.0",
"preferred": true,
"period": {
"start": "2015-08-21"
}
}
]
}
您可以通过简单地将代码元素与值 JPID 匹配来查找包含至少一个元素在 type.coding 数组中的所有文档:
db.system.find({
"type.coding.code": "JPID"
})
建议在 {"type.coding.code":1} 上创建和索引,以便您的搜索在此字段上执行得更快。
如果您需要查找所有文档并仅过滤具有代码“JPID”的数组条目,您可以使用 $filter 如下:
db.system.aggregate([
{
$match: {
"type.coding.code": "JPID"
}
},
{
$addFields: {
"type.coding": {
"$filter": {
"input": "$type.coding",
"as": "tc",
"cond": {
$eq: [
"$$tc.code",
"JPID"
]
}
}
}
}
}
])
我是 MongoDB 查询的新手,正在寻找有关如何检索符合搜索条件的文档的一些指导。特别是,我需要找到代码为 JPID 的文档的所有 id(s),即 "code": "JPID" .我们可以假设集合的名称是 systems.
因为这是一个嵌套对象,所以我想使用 $unwid - 但我仍然不知道如何去做。这个例子非常简单。感谢任何帮助和指导。
{
"resourceType": "NamingSystem",
"id": "example-id",
"name": "Austalian Healthcare Identifier - Individual",
"status": "active",
"kind": "identifier",
"date": "2015-08-31",
"publisher": "HL7 Australia on behalf of NEHTA",
"responsible": "HI Service Operator / NEHTA",
"type": {
"coding": [
{
"system": "http://hl7.org/fhir/v2/0203",
"code": "JPID";
"display": "National unique individual identifier"
}
],
"text": "IHI"
},
"description": "Australian HI Identifier as established by relevant regulations etc",
"uniqueId": [
{
"type": "oid",
"value": "1.2.36.1.2001.1003.0",
"comment": "This value is used in Australian CDA documents"
},
{
"type": "uri",
"value": "http://ns.electronichealth.net.au/id/hi/ihi/1.0",
"preferred": true,
"period": {
"start": "2015-08-21"
}
}
]
}
您可以通过简单地将代码元素与值 JPID 匹配来查找包含至少一个元素在 type.coding 数组中的所有文档:
db.system.find({ "type.coding.code": "JPID" })
建议在 {"type.coding.code":1} 上创建和索引,以便您的搜索在此字段上执行得更快。
如果您需要查找所有文档并仅过滤具有代码“JPID”的数组条目,您可以使用 $filter 如下:
db.system.aggregate([ { $match: { "type.coding.code": "JPID" } }, { $addFields: { "type.coding": { "$filter": { "input": "$type.coding", "as": "tc", "cond": { $eq: [ "$$tc.code", "JPID" ] } } } } } ])