Node.js 如何对来自Firebase的传入数据进行排序/排序

Node.js 如何对来自Firebase的传入数据进行排序/排序,node.js,json,firebase,google-cloud-firestore,Node.js,Json,Firebase,Google Cloud Firestore,我正在做一个firebase和node.js Express项目,它基本上是一个订购系统 我想按时间对收到的订单进行排序。我尝试使用firebase提供的Orderby()函数,但它仍然会扰乱json输出中的顺序 看来这应该行得通,我不必在我的前端对它进行排序 这是我去火场的路线 // route for database router.get("/orderslist", (req, res) => { let orders = [] let number_of_or

我正在做一个firebase和node.js Express项目,它基本上是一个订购系统

我想按时间对收到的订单进行排序。我尝试使用firebase提供的Orderby()函数,但它仍然会扰乱json输出中的顺序

看来这应该行得通,我不必在我的前端对它进行排序

这是我去火场的路线

// route for database 
router.get("/orderslist", (req, res) => {    

  let orders = []
  let number_of_orders

    // Getting the snapshot of the order collection
    db.collection('orders').orderBy('time').get().then( productSnapshot => {
      number_of_orders = productSnapshot.size

      // iterating over the order snapshot
      productSnapshot.forEach(orderDoc => {

        // creating an order object and assigning the ID and the rest of the information from the database
        var order = {
          id: orderDoc.id,
          customer_name: orderDoc.data().customer_name,
          date: orderDoc.data().date,
          comments: orderDoc.data().comments,
          time: orderDoc.data().time,
          total: orderDoc.data().total,
          order_status: orderDoc.data().order_status,
          products: []
        }
        // using the id, to get the products from the subcollection
        db.collection('orders/' + order.id + '/products').get().then( productSnapshot => {

          // iterating over the product snapshot
          productSnapshot.forEach(productDoc => {
            // creating a product object
            var product = {
              name: productDoc.data().name,
              price: productDoc.data().price
            }

            // then we push the product object to the list of products in the order object
            order.products.push(product)

          });

          // we are finished iterating over the productsnapshot and now we can push it to the orders list
          orders.push(order)

          // checks if the list is filled with everything from the database
          if(orders.length == number_of_orders){
            return res.json(orders)
          }

        });

      });

  });

});
这是json中的数据

[
  {
    "id": "xWmlB9fHD4rw8Di75llp",
    "customer_name": "Emil",
    "date": "07/05-2020",
    "comments": "without sugar",
    "time": "a",
    "total": 40,
    "order_status": true,
    "products": [
      {
        "name": "Latte",
        "price": 40
      }
    ]
  },
  {
    "id": "GWJoxe0DVFDWfi2AW6jS",
    "customer_name": "test",
    "date": "222222",
    "comments": "",
    "time": "f",
    "total": 10,
    "order_status": false,
    "products": [
      {}
    ]
  },
  {
    "id": "ggYSVKA1i0U8khIpeud4",
    "customer_name": "Oliver",
    "date": "01/05-2020",
    "comments": "order to be packed neatly because im on a bike",
    "time": "d",
    "total": 38,
    "order_status": false,
    "products": [
      {
        "name": "Latte Macchiato",
        "price": 38
      }
    ]
  }
]


我建议您创建一个适当的Firestore时间戳字段作为引用,而不是您称为“时间”的字符串字段

话虽如此,了解Firestore时间戳可能很棘手,以下是如何使用它们

当您从Firestore获得时间戳时,它们的类型如下:

要将其转换为普通时间戳,可以使用.toDate()函数

例如,对于以下文档:

db.collection('[COLLECTION]')
            .orderBy("timeStamp", Query.Direction.ASCENDING)

我们可以使用类似于:

db.collection('[COLLECTION]').doc('[DOCUMENT]').get().then(function(doc) {
  console.log(doc.data().[FIELD].toDate());
});
输出结果如下所示:

2019-12-16T16:27:33.031Z
现在,为了进一步处理该时间戳,您可以将其转换为字符串,并使用正则表达式根据需要对其进行修改

例如:(我在这里使用Node.js)

将为您提供如下输出:

现在,您可以对查询本身使用orderby方法,或者在前端获取它们之后使用orderby方法

orderby示例如下所示:

db.collection('[COLLECTION]')
            .orderBy("timeStamp", Query.Direction.ASCENDING)

但是,如果您计划继续使用那里的字符串类型(不推荐),则必须将该字符串转换为数值(可能是ASCII)要能够将其作为时间对象进行正确筛选。

我建议添加一个unix时间戳字段,然后您可以按它排序。@i按时间排序,这是用于测试的数据类型编号,但仍然不起作用。看起来“时间”是字符串而不是数字。