Home > Enterprise >  Get Collections and sub collection of documents
Get Collections and sub collection of documents

Time:04-11

I need to get the collection and its subcollection of all documents in a collection in Firestore.

If I am to create a json of it, it will look like this

{
    "Plazas": {
        "Doc.Id": "Plaza1"
        "NumberOfLanes": 4,
        "PlazaName": "My Plaza Name"
        "ServerIp": "127.0.0.1"
        "Lanes": {
            "Doc.Id": "Lane1",
            "IpAddress": "127.0.0.2",
            "IsExit": true,
            "Softwares": {
                "Doc.Id": "Software1",
                "LastUpdate": "2022-01-01",
                "Version": 1.0.0.0
            }
        }
    }
} 

These are my classes:

[FirestoreData]
public class Plaza
{
    public string? DocId { get; set; } = null;
    [FirestoreProperty]
    public int NumberOfLanes { get; set; }
    [FirestoreProperty]
    public string? PlazaName { get; set; } = null;
    [FirestoreProperty]
    public string? ServerIp { get; set; } = null;
    public DateTime? DateCreated { get; set; }
    [FirestoreProperty]
    public List<Lane> Lanes { get; set; } = new List<Lane>() { };
}

[FirestoreData]
public class Lane
{
    public string? DocId { get; set; } = null;
    [FirestoreProperty]
    public string? IpAddress { get; set; }
    [FirestoreProperty]
    public bool IsExit { get; set; }
    public List<Software> Softwares { get; set; } = new List<Software> { };
}

[FirestoreData]
public class Software
{
    public string? DocId { get; set; } = null;
    [FirestoreProperty]
    public DateTime? LastUpdate { get; set; }
    [FirestoreProperty]
    public string? Version { get; set; } = null;
}

and this is how I get all of my data:

public async Task<List<Plaza>> GetAllPlaza()
{
    try
    {
        var plazaQuery = firestoreDb.Collection("Plazas");
        var plazaQuerySnapshot = await plazaQuery.GetSnapshotAsync();
        var plazaList = new List<Plaza>();
        foreach(var documentSnapshot in plazaQuerySnapshot)
        {
            
            if (documentSnapshot.Exists)
            {
                Dictionary<string, object> plaza = documentSnapshot.ToDictionary();
                var json = JsonConvert.SerializeObject(plaza);
                var newPlaza = JsonConvert.DeserializeObject<Plaza>(json);
                newPlaza.DocId = documentSnapshot.Id;
                newPlaza.DateCreated = documentSnapshot.CreateTime.Value.ToDateTime();
                plazaList.Add(newPlaza);
            }
        }
        var storedPlazaList = plazaList.OrderBy(x => x.DocId).ToList();
        return storedPlazaList;
    }
    catch (Exception ex)
    {
        throw;
    }
}

On running this is the only data I get:

{
    "PlazaName":"My Plaza Name",
    "NumberOfLanes":4,
    "ServerIp":"127.0.0.1"
}

And when I desrialize, the "Lanes" property count is 0 (because it cannot get the Lanes from firestore).

CodePudding user response:

Read operations on Firestore are always shallow, so if the Lanes in your example data is a subcollection it is indeed expected that this is not automatically read too. You will need to perform a separate read operation on the Lanes subcollection to get those documentations, after reading the parent document. You can get the relevant CollectionReference from documentSnapshot.Reference.Collection("Lanes").

  • Related