如何在firestore中构建订单数据?

阿尤什·库马尔

我正在制作一个送餐应用程序。我在查询订单 Id订单状态的数据遇到障碍

这是我的数据库设计:

Users (collections)
------> user id (documents)
----------> orders(object)

// Orders is a nested object. You can find an image of this down below.
orders{
   "order_id":{
     created_at: <time>
     order_items: <array>
     status: <string> 
  }
}

有什么方法可以接收待处理商品的订单状态?

这是数据库的图像Firestore 数据库图像

达马拉吉

最好orders将其设为子集合(或单独的根级别集合),以便您可以查询具有特定状态的订单。数据库结构如下:

users -> {userId} -> orders -> {orderId}
(col)     (doc)       (col)      (doc)

否则,使用当前结构,您将必须使用 Javascript 获取完整的文档和排序顺序。另请注意,单个文档有1 MB 的限制,因此您限制了用户可以拥有的订单数量。

将每个订单移动到“订单”子集合中的文档后,您可以像这样查询待处理订单:

const q = query(collection(db, "users", userId, "orders"), where("status", "==", pending));

const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
  console.log(doc.id, " => ", doc.data());
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章