Go 如何在查询中使用Firestore DocumentID\uuuuu name\uuuuuuuu
我计划使用in查询选择器根据ID列表返回多个文档。但是,对于这个示例,我已经简化为使用==:Go 如何在查询中使用Firestore DocumentID\uuuuu name\uuuuuuuu,go,google-cloud-firestore,gcloud,Go,Google Cloud Firestore,Gcloud,我计划使用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) } // He
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))
输出:
ref.ID NF3CCjDikC9iHPubGA8o
len(allDocs): 1
allDocs[0].Ref.ID NF3CCjDikC9iHPubGA8o
len(docQuery): 0
据我所知,上面的代码应该在查询中返回一个文档。我是否错误地使用了DocumentID ____; name __;选择器?好的,我已经解决了。将最终查询更改为:
Where(firestore.DocumentID, "==", ref)
。。。e、 g.传递整个*firestore.DocumentRef而不是字符串在emulator和生产firestore中都有效
在这两种环境中传递字符串都不会返回结果,但在使用emulator时,它会自动失败并且不会返回错误。好的,我已经解决了。将最终查询更改为:
Where(firestore.DocumentID, "==", ref)
。。。e、 g.传递整个*firestore.DocumentRef而不是字符串在emulator和生产firestore中都有效
在这两种环境中传递字符串都不会返回结果,但使用模拟器时会自动失败且不会返回错误。您是如何定义在firestore.DocumentID中使用的firestore的?DocumentID是firestore包中的常量字符串,其值为_name\uu请尝试删除.GetAll查询。让我知道它是否有效。这不起作用。。。迭代器返回iterator.Done.OK,以便了解更多信息。我正在使用firestore模拟器进行此测试。我刚刚试着部署到生产中,我从上一个查询中得到以下错误:rpc error:code=InvalidArgument desc=\uuuuu key\uuuu筛选器值必须是一个键您是如何定义在firestore.DocumentID中使用的firestore的?DocumentID是firestore包中的常量字符串,其值为u name\uu请尝试删除.GetAll查询。让我知道它是否有效。这不起作用。。。迭代器返回iterator.Done.OK,以便了解更多信息。我正在使用firestore模拟器进行此测试。我刚刚尝试部署到生产环境中,从上一个查询中得到以下错误:rpc错误:code=InvalidArgument desc=\u_key\u_筛选器值必须是键