具有FieldPath.DocumentId的Firestore WhereIn查询引发异常 [英] Firestore WhereIn Query with FieldPath.DocumentId is throwing exception

查看:52
本文介绍了具有FieldPath.DocumentId的Firestore WhereIn查询引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试基于DocumentID列表从Collection中获取少量文档,并且无法使用WhereIn和FieldPath进行以下工作. Nuget版本Google.Cloud.Firestore v1.1.0

Trying to fetch few documents from a Collection based on a list of DocumentIDs and not able to get the following working using WhereIn and FieldPath. Nuget version Google.Cloud.Firestore v1.1.0

public async Task<IEnumerable<T>> GetByDocumentIdWhereIn(IEnumerable<string> documentIds)
{
    CollectionReference ref= FirestoreDb.Collection(_collectionName);
    Query query = ref.WhereIn(FieldPath.DocumentId, documentIds);
    QuerySnapshot querySnapshot = await query.GetSnapshotAsync();
    ...
}

同时使用几个documentId执行时,出现以下错误.

And I am getting the following error when executed with couple of documentIds.

RpcException: Status(StatusCode=InvalidArgument, Detail="__key__ filter value must be a Key")
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Grpc.Core.Internal.ClientResponseStream+<MoveNext>d__5.MoveNext() in ClientResponseStream.cs
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Linq.AsyncEnumerable+<ForEachAsync_>d__174.MoveNext() in ForEach.cs
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Google.Cloud.Firestore.Query+<GetSnapshotAsync>d__54.MoveNext() in Query.cs
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()

有什么想法吗?

推荐答案

注意:此答案是在Google.Cloud.Firestore 2.1.0发行之前编写的.从2.1.0版本开始,原始代码应该可以使用.

Note: this answer was written before the release of Google.Cloud.Firestore 2.1.0. As of 2.1.0, the original code should work.

这是服务器生成的异常,但是可以在客户端转换查询以使其起作用.如果提供的值是DocumentReference值而不是字符串,则查询有效.

This is a server-generated exception, but it's possible to transform the query on the client-side so that it works. If the values provided are DocumentReference values instead of just strings, the query works.

这意味着现在您可以像这样修复代码:

That means right now you can fix your code like this:

CollectionReference coll = FirestoreDb.Collection(_collectionName);
var docRefs = documentIds.Select(id => coll.Document(id)).ToList();
Query query = coll.WhereIn(FieldPath.DocumentId, docRefs);
QuerySnapshot querySnapshot = await query.GetSnapshotAsync();

将来,我们希望为您自动执行此操作;在此GitHub问题上将对此进行跟踪.

In the future we hope to do this for you automatically; progress on this will be tracked on this GitHub issue.

请注意,如果您只是想获取文档快照的集合,并且拥有(或可以创建)一系列DocumentReference值,则可以选择使用FirestoreDb.GetAllSnapshotsAsync.

Note that if you just want to fetch a collection of document snapshots and you have (or can create) a sequence of DocumentReference values, an alternative is to use FirestoreDb.GetAllSnapshotsAsync.

这篇关于具有FieldPath.DocumentId的Firestore WhereIn查询引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆