通过 ID 查询 Firebase Firestore 文档 [英] Query Firebase Firestore documents by the ID

查看:34
本文介绍了通过 ID 查询 Firebase Firestore 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我从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)?

例如,集合Things"中的文档具有 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆