简体   繁体   English

C# FireStore,从文档中获取值

[英]C# FireStore, get values from a document

I'm trying to get the values inside of a document in FireStore, so I did this.我试图在 FireStore 中的文档中获取值,所以我这样做了。

Task<QuerySnapshot> docRef = db.Collection("Logs").GetSnapshotAsync();
foreach (DocumentSnapshot doc in await docRef){
   DocumentSnapshot log = await db.Collection("Logs").Document(doc.Id).GetSnapshotAsync();
   Console.WriteLine(log); // How to get the data in log?
}

Thing is that I don't know how to get the Data in log.问题是我不知道如何获取日志中的数据。 I tried log["infoLog"]["name"] but it doesn't work.我试过log["infoLog"]["name"]但它不起作用。

As you can see, the data contains a Map (infoLog) and inside of that, different strings, integers etc..如您所见,数据包含一个 Map (infoLog),其中包含不同的字符串、整数等。

What would be the right way?什么是正确的方法? Thanks!谢谢!

Updated更新

Tried this试过这个

        Task<QuerySnapshot> docRef = db.Collection("Logs").GetSnapshotAsync();
            foreach (DocumentSnapshot doc in await docRef)
            {
                DocumentSnapshot log = await db.Collection("Logs").Document(doc.Id).GetSnapshotAsync();

                foreach (KeyValuePair<string, object> pair in log.ToDictionary())
                {
                    Console.WriteLine("{0}: {1}", pair.Key, pair.Value);

                }
            }

But the data didn't come in order.但数据并没有按顺序排列。 (Because asynchronous I assume) (因为我假设是异步的)

You can get the documents already ordered from the Firestore side by using the .OrderBy("FIELD_NAME") or OrderByDescending("FIELD_NAME") functions when querying for the data as mentioned in the official documentation .在查询官方文档中提到的数据时,您可以使用.OrderBy("FIELD_NAME")OrderByDescending("FIELD_NAME")函数从 Firestore 端获取已排序的文档。 This way you will get them already ordered so you won't need to order them in client side.这样你就可以得到它们已经订购了,所以你不需要在客户端订购它们。

Task<QuerySnapshot> docRef = db.Collection("Logs").OrderBy("FIELD_NAME").GetSnapshotAsync();
    foreach (DocumentSnapshot doc in await docRef)
    {
        DocumentSnapshot log = await db.Collection("Logs").Document(doc.Id).GetSnapshotAsync();
        foreach (KeyValuePair<string, object> pair in log.ToDictionary())
        {
            Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
        }
    }

Just need to replace FIELD_NAME with the name of the field you want to order by.只需将FIELD_NAME替换为您要订购的字段的名称。 In case you want to order by more than one field it can be done as well just by using OrderBy() multiple times.如果您想按多个字段排序,也可以通过多次使用OrderBy()来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM