Dav*_*phy 1 go gcloud google-cloud-firestore
我计划使用“in”查询选择器根据 ID 列表返回多个文档。不过,在此示例中我简化为使用“==”:
collection := server.Firestore.Collection("foo")
// Add a document:
ref, _, err := collection.Add(ctx, map[string]string{"a": "b"})
if err != nil {
panic(err)
}
// Here's the document ID:
fmt.Println("ref.ID", ref.ID)
// Get all the documents in the collection just to check it's there:
allDocs, err := collection.Query.Documents(ctx).GetAll()
if err != nil {
panic(err)
}
fmt.Println("len(allDocs):", len(allDocs))
// Check our document is the one in the collection:
fmt.Println("allDocs[0].Ref.ID", allDocs[0].Ref.ID)
// Get the document using __name__ query:
docQuery, err := collection.Query.Where(firestore.DocumentID, "==", ref.ID).Documents(ctx).GetAll()
if err != nil {
panic(err)
}
fmt.Println("len(docQuery):", len(docQuery))
Run Code Online (Sandbox Code Playgroud)
输出:
ref.ID NF3CCjDikC9iHPubGA8o
len(allDocs): 1
allDocs[0].Ref.ID NF3CCjDikC9iHPubGA8o
len(docQuery): 0
Run Code Online (Sandbox Code Playgroud)
据我所知,上面的代码应该在查询中返回一个文档。我是否错误地使用了 DocumentID(“__ name __”)选择器?
好的,我已经解决了。将最终查询更改为:
Where(firestore.DocumentID, "==", ref)
Run Code Online (Sandbox Code Playgroud)
...例如传递整个*firestore.DocumentRef而不是string模拟器和生产 Firestore 中的作品。
传递字符串在两种环境中都不会返回结果,但在使用模拟器时,它会默默失败并且不返回任何错误。