简体   繁体   English

在过去 24 小时内从 Firestore 中的集合中获取最大文档

[英]Getting biggest document from a collection in the last 24 hours in Firestore

I have a collection of trades in my Firestore database.我的 Firestore 数据库中有一组交易。 The trades have the following relevant data that I need in the query:这些交易具有我在查询中需要的以下相关数据:

time: 1589535935410 (milliseconds)
tradeValue: 12343.017
isBuyerMaker: true || false

I need to run a query that gets the biggest TradeValue (one in isBuyerMaker true and also in false) in the last 24 hours.我需要运行一个查询,以获取过去 24 小时内最大的 TradeValue(一个在 isBuyerMaker 中为 true,也为 false)。 How can I achieve this through Firestore queries?如何通过 Firestore 查询实现此目的?

I tried running this query:我尝试运行此查询:

const biggestBuyQuery = firebase
    .firestore()
    .collection("cex-trades")
    .orderBy("time", "desc")
    .where("isBuyerMaker", "==", true)
    .where("time", ">", seconds24h())
    .orderBy("tradeValue", "desc")
    .limit(1); 

This does get the documents from the last 24 hours but since it is being ordered by time, I do not get the biggest tradeValue这确实获得了过去 24 小时的文件,但由于它是按时间排序的,所以我没有获得最大的 tradeValue

From the tags I'm going to assume you're working with the JS firestore API.根据标签,我假设您正在使用 JS firestore API。

Edit : I've added workaround that should work for this:编辑:我添加了适用于此的解决方法:

let query = collection
    .where("isBuyerMaker", "==", /* true / false */)
    .where("time", ">", /* now.milliseconds - (24*60*60*1000) */)
    .where("tradeValue", ">=", 0) // New workaround
    .orderBy("tradeValue", "desc")
    .limit(1)
    .get()

I'd run a compound query with a few where clauses:我会运行一个带有几个 where 子句的复合查询:

let query = collection
    .where("isBuyerMaker", "==", /* true / false */)
    .where("time", ">", /* now.milliseconds - (24*60*60*1000) */)
    .orderBy("tradeValue", "desc")
    .limit(1)
    .get()

and collect the 0th document from the promise when it resolved并在解决时从 promise 中收集第 0 个文档

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

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