按周排列数据'在Firebase / Firestore中

时间:2018-03-02 11:27:01

标签: firebase database-design nosql google-cloud-firestore data-modeling

我试图设计我的商店数据结构来存储我们设置为特定周的购物清单。

这是我的初步方法:

public class BasketDbContext : DbContext
{
    public BasketDbContext()
        : base("BasketDb")
    {

    }

    public DbSet<Player> Player { get; set; }
    public DbSet<BasketBallClub> BasketBallClub { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Configurations.Add(new PlayerMap());
        modelBuilder.Configurations.Add(new BasketBallClubMap());
    }

}

using System.Data.Entity.ModelConfiguration;
public class PessoaMap : EntityTypeConfiguration<Player>
{
    public PlayerMap()
    {
        // Primary Key
        HasKey(t => t.Id);

        // Properties
        Property(t => t.PlayerName).HasMaxLength(50); //.HasColumnName("player_name");
        Property(t => t.PlayerSurname).HasMaxLength(50);
        Property(t => t.PlayerWeight);
        //..... configure all columns 

        //Relationships

        HasRequired(t => t.BasketBallClub)
                .WithMany(t => t.Players)
                .HasForeignKey(d => d.BasketBallClubId)
                .WillCascadeOnDelete(false);
    }
}

using System.Data.Entity.ModelConfiguration;
public class BasketBallClubMap : EntityTypeConfiguration<BasketBallClub>
{
    public BasketBallClubMap()
    {
        // Primary Key
        HasKey(t => t.Id);

        // Properties
        Property(t => t.Id); //.HasColumnName("id");
        Property(t => t.ClubName).HasMaxLength(50); //.HasColumnName("club_name");
    }
}

// Your Model
public class PlayerIndex1Model
{
    public int Id { get; set; }
    public string PlayerName { get; set; }
    public string PlayerSurname { get; set; }
    //... omitted other columns 
    public int BasketBallClubId { get; set; }
    public virtual BasketBallClub BasketBallClub { get; set; }
}

public class BasketBallClub
{
    public int Id { get; set; }
    public string ClubName { get; set; }

    public virtual IColletion<Player> Players { get;set; } 
}

然后我会查询// api/users { user: 'Karl Taylor', userId: 43 shoppingListId: 1 }

我打算将周参赛作为本周的第一天。例如,2018年3月19日星期一,其中包括从19日星期一到25日星期日的一切。

api/shopping_lists/1

这种数据结构的任何好资源都会很棒。

2 个答案:

答案 0 :(得分:2)

你可以这样做:

Users
  useruid
    name:userx
    email:userx@gmail.com
  useruid
    name:usery
    email:usery@gmail.com

Daysoftheweek
     randomid
        date: 2018-03-19
        item1:eggs
        item2:popcorn
        username: userx
      randomid
         date:2018-03-26
         item1:pizza
         item2: pepsi
         username:userx

然后您可以查询orderByChild("date").equalTo(2018-03-19),然后您将获得该周的项目列表

答案 1 :(得分:0)

自然的结构方法是在用户下面建立一个列表子集合。

所以一个例子是:

Collection: /users
  -> Document: <auto id> (acts as user ID)
     {
       user: 'Karl Taylor'
     }
     Collection /lists
     -> Document: "2018-03-19"
        {
          items: ['milk', 'eggs']
        }
     -> Document: "2018-03-26"
        {
          items: ['pasta', 'cheese']
        }

在控制台中,users集合如下所示: dropped and replaced

lists集合如下所示: View of the users collection in the Firebase Console

这允许您获得用户的所有周列表:

db.collection("users").doc(userid).collection("lists")
  .get().then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
      // doc.data() is never undefined for query doc snapshots
      console.log(doc.id, " => ", doc.data());
    });
  })
  .catch(function(error) {
    console.log("Error getting documents: ", error);
  });

或者只是一个包含查找的特定列表:

db.collection("users").doc(userid).collection("lists").doc("2018-03-19")
  .get().then(function(doc) {
    if (doc.exists) {
      console.log("Document data:", doc.data());
    } else {
      // doc.data() will be undefined in this case
      console.log("No such document!");
    }
  }).catch(function(error) {
    console.log("Error getting document:", error);
  });