通过ID查询Firebase Firestore文档 [英] Query Firebase Firestore documents by the ID
问题描述
正如我从"Cloud Firestore数据模型"指南中获得的每个文件都由名称标识"一样.可以通过该文档标识符(名称或ID)查询集合吗?
As I got from 'Cloud Firestore Data Model' guide "each document is identified by a name." Is it possible to query a collection by that document identifier (which is the name or ID)?
例如,事物"集合中的文档具有ID:0、1、2等:
For example, documents in the collection "Things" have IDs: 0, 1, 2 etc.:
是否可以查询ID小于100的文档?
Is it possible to query documents which IDs are less than 100?
推荐答案
您可以使用特殊的哨兵FieldPath.documentId()
通过documentId查询,例如:
You can query by documentId using the special sentinel FieldPath.documentId()
, e.g.:
const querySnap = collection.where(firebase.firestore.FieldPath.documentId(), '<', '100').get();
但是请注意,文档ID是字符串,因此,这将包括ID为'0'或'1'的文档,而不是包含'2'的文档,因为按字典顺序'2'>'100'.
But be aware that document IDs are strings and therefore this will include documents with ID '0' or '1', but not '2' since '2' > '100' lexicographically.
因此,如果要进行数字查询,则需要将文档ID作为文档中的数字字段编写,然后对其进行常规查询.
So if you want a numeric query, you'll need to write the document ID as a numeric field in the document and then do a normal query on it.
这篇关于通过ID查询Firebase Firestore文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!