简体   繁体   English

Cloud Firestore 数据查询

[英]Cloud Firestore Data Query

I am trying to query the below data structure for only documents that contain a map with a certain key.我正在尝试仅查询包含具有特定键的地图的文档的以下数据结构。 For example, I would want to query the collection "user-picks" only for documents which a contain a field with key "2020-09-16T12:10:00-05:00-987346-10923" .例如,我只想查询包含键为"2020-09-16T12:10:00-05:00-987346-10923"的字段的文档的集合"user-picks "

在此处输入图片说明

Is this possible in Node.js?这在 Node.js 中可行吗?

I have tried:我试过了:

const userPicksRef = firestore.collection('user-picks').where('gameId', '==', String(gameId))
const snapshot = await userPicksRef.get();
snapshot.forEach(doc => {
    console.log(doc.id, " => ", doc.data());
});

After verifying the gameId was correct this returned nothing.验证 gameId 正确后,这没有返回任何内容。 I would rather query off the key of the field than the gameId though, although they are the same?我宁愿查询字段的键而不是 gameId,尽管它们是相同的?

Your query isn't going to work because you haven't identified the full path of the field that contains the data you want.您的查询将不起作用,因为您尚未确定包含所需数据的字段的完整路径。 Your fields are nested in maps, and Firestore requires that you know the full name of the nested field in order to use it in a query.您的字段嵌套在地图中,Firestore 要求您知道嵌套字段的全名,以便在查询中使用它。 So, the only way you can find a certain game ID is to also know the name of the field with the formatted date, and reference it with dot notation .因此,您可以找到某个游戏 ID 的唯一方法是同时知道带有格式化日期的字段名称,并使用点符号引用它。 Firestore will not wildcard any part of the full field name, or search deeply in fields for you. Firestore 不会通配完整字段名称的任何部分,也不会为您深入搜索字段。

If you don't know the full name of the field to use for an equality query, you will have to restructure your data.如果您不知道用于等式查询的字段的全名,则必须重构数据。 If you want to be able to search for any game ID that was added to a document, you could add a new array field and populate it with the game ID strings that you want to find.如果您希望能够搜索添加到文档中的任何游戏 ID,您可以添加一个新的数组字段并使用您想要查找的游戏 ID 字符串填充它。 Duplicating data like this to satisfy a specific query is common in Firestore.像这样复制数据以满足特定查询在 Firestore 中很常见。

If you had a new top-level array field called gameIds populated with strings, you could query it with an array-contains query:如果您有一个名为gameIds的新顶级数组字段, gameIds填充了字符串,则可以使用array-contains查询来查询它:

const userPicksRef = firestore
    .collection('user-picks')
    .where('gameIds', 'array-contains', gameId)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM