按文档id进行Firestore结构化查询搜索



我试图通过文档id查询firestore集合,但不是使用firestore CLI,而是使用结构化查询(因为我使用Zapier来自动化一些工作流(。有没有一种方法可以通过记录作为字段进行搜索?到目前为止,我已经尝试了以下代码和变体,其中我将name替换为";documentId(("documentId";似乎什么都不起作用。当我使用名称时,我会得到以下错误:

"错误":"代码":400,"消息":">key过滤器值必须是key";,"状态":"INVALID_ARGUMENT";

"where": {
"fieldFilter": {
"field": {
"fieldPath": "__name__"
},
"op": "EQUAL",
"value": {
"stringValue": "THE ID I WANT TO LOOK FOR"
}
}
}

如果要使用__name__按id获取文档,请将值类型更改为referenceValue,并提供文档的完整路径,而不仅仅是查询的id。

"where": {
"fieldFilter": {
"field": {
"fieldPath": "__name__"
},
"op": "EQUAL",
"value": {
"referenceValue": "projects/project_id/databases/database_id/documents/your_collection/doc_id"
}
}
}

假设您想使用文档id获取特定文档。您可能需要参考以下代码:

axios.get(`https://firestore.googleapis.com/v1/projects/<Project-ID>/databases/(default)/documents/<Collection-Name>/<Document-ID>`)
.then(res => {
console.log(res);
})
.catch(error => {
console.log(error);
});

上面的代码将获得您想要的特定文档。如果您想获得一个具有特定字段值的文档ID,那么这里的这一行:"fieldPath": "__name__"应该是fieldName,而不是文档ID。"fieldPath": "<FieldName>"。此外,请确保您使用的方法是POST。参考以下代码示例:

axios.post(`https://firestore.googleapis.com/v1/projects/<Project-ID>/databases/(default)/documents:runQuery`,
{
"structuredQuery": {
"from": [{
"collectionId": "<Collection-Name>"
}],
"where": {
"fieldFilter": {
"field": {
"fieldPath": "<FieldName>"
},
"op": "EQUAL",
"value": {
stringValue: "<FieldValue>"
}
}
}
}
}).then(res => { 
console.log(res) 
})
.catch(error => { 
console.log(error) 
})

以上所有代码仅供参考,我使用axios从Firestore获取数据。

有关详细信息,请查看此文档。

最新更新