如何在Cloud Firestore创建子集合索引?

时间:2017-10-31 06:35:05

标签: firebase google-cloud-firestore

我已经Google搜索并检查了Cloud Firestore文档,但未发现有关如何声明子集合索引的信息。我宣布了这样的事情

...{    
  "collectionId": "user/{uid}/feeds",
  "fields": [..]
}...

并在索引标签中将其存储为

__escuser~1{uid}~1feeds__

不知道我是否正确创造了它。

2 个答案:

答案 0 :(得分:10)

当你去创建一个索引时,它实际上告诉你要手动运行你想要创建一个索引的查询,然后它会生成一个你可以将粘贴复制到浏览器中的URL等等。

enter image description here

您就是这样做的:

  1. 创建新目录
  2. npm init
  3. npm i --save firebase-admin
  4. 创建index.js
  5. 将以下功能放入文件

    const admin = require('firebase-admin');
    const serviceAccount = require('./firebase-creds.json');
    
    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: 'https://projectId.firebaseio.com'
    });
    
    function runQuery() {
      db
      .collection('users')
      .doc(someRandomUserId)
      .collection('feed')
      .where('read', '==', false)
      .where('timestamp', '<=', 1509889854742) //Or something else
      .get()
      .then(doc => {
        console.log(doc.data());
      })
      .catch(error => console.log(error));
    };
    runQuery();
    
  6. 运行node index.js

  7. 这会吐出这样的东西:

    { Error: The query requires an index. You can create it here: https://console.firebase.google.com/project/project-projectID/database/firestore/indexes?create_index=longRandomString ...}

    复制链接并将其粘贴到浏览器中。

    enter image description here

    <强>更新

    要手动添加索引(通过CLI),您可以执行以下操作:

    {
      "indexes": [
        {
          "collectionId": "feed",
          "fields": [
            { "fieldPath": "read", "mode": "ASCENDING" },
            { "fieldPath": "timestamp", "mode": "ASCENDING" },
            ...
          ]
        }
      ]
    }
    

    或者只需进入数据库中的管理面板,然后在那里添加源索引。

答案 1 :(得分:4)

@ michael-bleigh似乎在这里回答了这个问题:https://stackoverflow.com/a/47165007/2511985

  

Cloud Firestore索引基于集合名称,而不是完整的集合路径。因此,如果要在import java.util.Scanner; public class BakingCookies{ public static void main (String [] args) { int cookies; int ovenTemp; int cookTime; System.out.println("What is the size of your oven?\n(Your options are: Small, Medium & Large)"); Scanner in = new Scanner(System.in); String oven = in.nextLine(); System.out.println("How many cookies would you like to make?"); cookies = in.nextInt(); System.out.println("To make around "+ cookies +" cookies, please do the following:"); makeTheDough(oven); cookTheDough(oven); finishYourTreat(oven); } public static void makeTheDough(String oven){ int cupsofflour = 0; int cupsofbutter = 0; int teaspoonsfsugar = 0; int singleeggs = 0; if (oven.equals("Small")){ cupsofflour = 1; cupsofbutter = 1; teaspoonsfsugar = 5; singleeggs = 1; }else if (oven.equals ("Medium")){ cupsofflour = 2; cupsofbutter = 1; teaspoonsfsugar = 10; singleeggs = 2; }else if (oven.equals ("Large")){ cupsofflour = 3; cupsofbutter = 2; teaspoonsfsugar = 15; singleeggs = 3; } System.out.println("Preheat the oven to the degrees stated below. Mix together:"+ cupsofflour +"cups of flour,"+ cupsofbutter+ "cups of butter," + teaspoonsfsugar + "teaspoons of sugar, and"+ singleeggs +"eggs Then knead the dough to make a thick, but flexible piece of dough we will now shape into cookies!"); } public static void cookTheDough (String oven){ int cookies= 0; int ovenTemp= 0; int cookTime= 0 ; if (oven.equals("Small")){ cookies= 24; ovenTemp= 330; cookTime=30 ; }else if (oven.equals("Medium")){ cookies = 12; ovenTemp = 350; cookTime = 25; }else if (oven.equals("Large")){ cookies = 6; ovenTemp = 325; cookTime = 20; } System.out.println("take your dough and use the cut tool to make at the most"+cookies +"cookies. Place them on a tray, and stick them in the overat"+ ovenTemp + " degrees. Set your timer for"+ cookTime+"minutes"); } public static void finishYourTreat(String oven){ int coolTime = 10; if (oven.equals("small")){ coolTime = 10; } System.out.print("Watch them! when the timer is up, take them out and let them cool for " + coolTime + "minutes."); System.out.print("Your cookies are done! Eat and enjoy the fruits of your labor!"); } } 上创建索引,正确的方法是在users/{id}/messages上创建索引。