Sha*_*llo 13 firebase google-cloud-firestore
我用Google搜索并检查了Cloud Firestore文档,但没有发现如何声明子集合索引.我宣布过这样的话
...{
"collectionId": "user/{uid}/feeds",
"fields": [..]
}...
Run Code Online (Sandbox Code Playgroud)
在索引选项卡中,它存储如下
__escuser~1{uid}~1feeds__
Run Code Online (Sandbox Code Playgroud)
我不知道我是否正确创造了它.
Chr*_*ris 12
当你去创建一个索引时,它实际上告诉你要手动运行你想要创建索引的查询,然后它会生成一个你可以将粘贴复制到浏览器中的URL等等!
这是你如何做到的:
npm init
npm i --save firebase-admin
index.js
将以下功能放在文档中
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();
Run Code Online (Sandbox Code Playgroud)跑 node index.js
这会吐出这样的东西:
{ 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 ...}
复制链接并将其粘贴到浏览器中.
更新
要手动添加索引(通过CLI),您可以执行以下操作:
{
"indexes": [
{
"collectionId": "feed",
"fields": [
{ "fieldPath": "read", "mode": "ASCENDING" },
{ "fieldPath": "timestamp", "mode": "ASCENDING" },
...
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
或者只需进入数据库的管理面板,然后在那里添加feed的索引.
归档时间: |
|
查看次数: |
3780 次 |
最近记录: |