How can I get all the documents from that firestore collection that have at least one object with a doctorId value equal to 1 in a collection x

huangapple go评论75阅读模式
英文:

How can I get all the documents from that firestore collection that have at least one object with a doctorId value equal to 1 in a collection x

问题

我有一个包含文档的集合这些文档遵循以下模型

{
    "cardId": "12345678900",
    "phone": "123456789",
    "name": "Scott",
    "services": [
        {
            "name": "Estudios 3D para vías aéreas"
        },
        {
            "name": "Estudios 3D para ortodoncia"
        },
        {
            "name": "Estudios 3D para senos paranasales",
            "doctorId": "ASNDUasd981239nasd"
        },
        {
            "name": "Estudios 3D para implantes dentales",
            "pdfUrl": [],
            "imgUrl": [],
            "doctorId": 1
        },
        {
            "name": "Estudios 3D para vías aéreas",
            "imgUrl": [
                "https://firebasestorage.googleapis.com/v0/b/cid-imagenes.appspot.com/o/patients%2Fimages%2Frc-upload-1678379057443-9?alt=media&token=b156f890-aa2a-49e8-aa73-52260124b279"
            ],
            "pdfUrl": [
                "https://firebasestorage.googleapis.com/v0/b/cid-imagenes.appspot.com/o/patients%2Fdocuments%2Frc-upload-1678379057443-11?alt=media&token=c61378ba-413b-4e89-ba57-3a93e9f66a3f"
            ]
        }
    ],
    "lastname": "Castro",
    "birthdate": "Thu Mar 02 2023 15:43:23 GMT-0400 (hora estándar del Atlántico)",
    "gender": "M",
    "imgUrl": "",
    "type": "PATIENT",
    "email": "asdasd@asdasd.com",
    "referredById": 1
}

如何获取该集合中的所有文档,其中至少一个位于"services"数组内的对象包含一个doctorId属性,其值为1,在React中使用Firebase库。

我尝试使用以下方法:

query(ref, where('services', 'array-contains-any', { doctorId: 1 }));

但它不起作用,因为除了doctorId属性之外,"services"数组内的对象还有其他属性,所以它不匹配。


<details>
<summary>英文:</summary>

I have a collection with documents that follow the following model:


{
"cardId": "12345678900",
"phone": "123456789",
"name": "Scott",
"services": [
{
"name": "Estudios 3D para vías aéreas"
},
{
"name": "Estudios 3D para ortodoncia"
},
{
"name": "Estudios 3D para senos paranasales",
"doctorId": "ASNDUasd981239nasd"
},
{
"name": "Estudios 3D para implantes dentales",
"pdfUrl": [],
"imgUrl": [],
"doctorId": 1
},
{
"name": "Estudios 3D para vías aéreas",
"imgUrl": [
"https://firebasestorage.googleapis.com/v0/b/cid-imagenes.appspot.com/o/patients%2Fimages%2Frc-upload-1678379057443-9?alt=media&token=b156f890-aa2a-49e8-aa73-52260124b279"
],
"pdfUrl": [
"https://firebasestorage.googleapis.com/v0/b/cid-imagenes.appspot.com/o/patients%2Fdocuments%2Frc-upload-1678379057443-11?alt=media&token=c61378ba-413b-4e89-ba57-3a93e9f66a3f"
]
}
],
"lastname": "Castro",
"birthdate": "Thu Mar 02 2023 15:43:23 GMT-0400 (hora estándar del Atlántico)",
"gender": "M",
"imgUrl": "",
"type": "PATIENT",
"email": "asdasd@asdasd.com",
"referredById": 1
}

How can I get all the documents of that collection where at least one object inside the &quot;services&quot; array contains a doctorId property with the value of 1, the firebase library in react.

I tried using the method:

query(ref, where('services', 'array-contains-any', { doctorId: 1 }));


But it doesn&#39;t work, because apart from the doctorId property, the object inside the &quot;services&quot; array has other properties, so it doesn&#39;t match

</details>


# 答案1
**得分**: 0

`array-contains` 和 `array-contains-any` 运算符将指定的值与数组中的每个**整个**项进行比较,如果存在精确、完全匹配,则返回文档。无法用于部分匹配。

通常的解决方案,与NoSQL数据库一样,是添加额外的数据以支持用例。例如,在这里,您可以添加另一个顶层数组字段,其中包含所有医生的ID:

doctorIds: [1]


然后,您可以使用 `array-contains` 对该字段进行查询:

query(ref, where('doctorIds', 'array-contains', 1))


<details>
<summary>英文:</summary>

The `array-contains` and `array-contains-any` operators compare the value you specify against each **entire** item in the array, and returns the document if there is an exact, complete match. You can&#39;t use them for partial matches.

The common solution is, as usual with NoSQL databases, to add additional data to allow the use-case. Here for example, you can add another top-level array field with all the doctor IDs:

doctorIds: [1]


And then you can query on that field with `array-contains`:

query(ref, where('doctorIds', 'array-contains', 1))


</details>



huangapple
  • 本文由 发表于 2023年3月12日 11:45:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定