Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – Firestore get random documents of last 48 hours

Posted on November 14, 2022

Solution 1 :

I see two solutions:

  1. Assuming the posts are evenly distributed over the 48h, use the timestamp as random index as mentioned in the answer you are linking

  2. Code the timestamp in a way that you dont need to use a range to filter over the last 48h. For instance you could add a field timechunk that splits time in chunks of 5h and is incremented by 1 at every chunk and then just add to your query the last 10 chunks

    let postsRef = db.collection("posts")
    queryRef = postsRef.whereField("random", isGreaterThanOrEqualTo: lowValue)
                    .whereField("timechunk", in: ["chunk1", "chunk2", "chunk3", "chunk4", "chunk5", "chunk6", "chunk7", "chunk8", "chunk9", "chunk10"])
                    .order(by: "random")
                    .limit(to: 1)
    

This way you get a random post over the last 50h-ish. I am using this solution for a similar problem.

Problem :

In my situation, I need to read random posts of the last 48hours. I have seen the method where we need to create a random id and query with isGreaterThanOrEqualTo range to get random posts. But I also need to use it for the timestamp. As I know, Firestore doesn’t let range queries on different fields in one query. How can I get random posts of the last 48hours?

  --- posts (collection)                                                     
   |     |
   |     --- postid (documents)
   |          |
   |          --- country_code: "TR"
   |          |
   |          --- timestamp: "Server.Timestamp"
   |          |
   |          --- post_text: "Some Text" 

Comments

Comment posted by l1b3rty

Can you define clearly “random posts of the last 48hours”? What have you tried?

Comment posted by Nasir

App stores a post with auto-Id. Every post document has a timestamp, country_code fields. I am just trying to fetch random posts within a country but in 48hours and show them to the user. I will edit the question with my post’s data structure. Actually, I haven’t tried anything yet, because I clearly see that it is not possible with Firestore limitations.

READ  [ANSWERED] java - How to delete an audio file on android 11
Powered by Inline Related Posts

Comment posted by l1b3rty

And what do you mean by “I have seen the method where we need to create a random id and query with isGreaterThanOrEqualTo range to get random posts”?

Comment posted by stackoverflow.com/a/46801925/5817560

I mean this method:

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress