最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

firebase - Ordering Firestore documents using a nested field - Stack Overflow

matteradmin7PV0评论

Trying out Firestore REST API, the documentation states that to order items ?orderBy=name%20desc this orders using the document name in descending order. The documents I need to order have the following structure

{
  "name": "document_name",
  "fields":{
    "createTime": "2024-08-23T23:06:05.206209Z",
  }
}

How could I order using the createTime field? since doing ?orderBy=fields.createTime returns empty JSON.

Trying out Firestore REST API, the documentation states that to order items ?orderBy=name%20desc this orders using the document name in descending order. The documents I need to order have the following structure

{
  "name": "document_name",
  "fields":{
    "createTime": "2024-08-23T23:06:05.206209Z",
  }
}

How could I order using the createTime field? since doing ?orderBy=fields.createTime returns empty JSON.

Share Improve this question edited Nov 19, 2024 at 6:49 Alex Mamo 139k18 gold badges169 silver badges201 bronze badges asked Nov 17, 2024 at 6:43 mbithimbithi 375 bronze badges 3
  • Removed the google-cloud-firestore tag since using ?orderBy= in the query string is the way used to filter with the RTDB REST API – Renaud Tarnec Commented Nov 18, 2024 at 8:06
  • Hey mbithi. Is it the Realtime Database or Cloud Firestore? – Alex Mamo Commented Nov 18, 2024 at 8:16
  • it's cloud firestore – mbithi Commented Nov 19, 2024 at 5:12
Add a comment  | 

1 Answer 1

Reset to default 0

As far as I know, Firestore REST API doesn't allow ordering directly on nested fields (like fields.createTime) as you are trying to do right now. However, this technique will only work if you use mobile or web SDKs.

So to solve this, I recommend you modify the structure of your document so that createTime becomes a top-level field in the document and not a nested one:

{
  "name": "document_name",
  "createTime": "2024-08-23T23:06:05.206209Z", //
Post a comment

comment list (0)

  1. No comments so far