如何在Firebase Realtime Database中设置双重查询并实现多条件筛选?
CurrentUserId and ordering by timestamp in Firebase Realtime Database Hey there, let's break down how to fix your query issue! The key thing to remember about Firebase Realtime Database queries is that you can only use one orderBy method per query—so calling orderByChild twice like you did will just overwrite the first one, which is why your approach isn't working as expected.
Let's cover two common solutions depending on your data structure:
Option 1: Use a composite index field (if you keep your current data structure)
If your Users node has individual records each with CurrentUserId and timestamp fields (like below), you'll need to create a composite field to filter and sort in one go:
Users ├── record1 │ ├── CurrentUserId: "user_123" │ ├── timestamp: 1699999999 │ └── currentUserId_timestamp: "user_123_1699999999" └── record2 ├── CurrentUserId: "user_456" ├── timestamp: 1700000000 └── currentUserId_timestamp: "user_456_1700000000"
Step 1: Add the composite field to your records
When you write a new user record, include a field like currentUserId_timestamp that combines the user ID and timestamp (make sure the timestamp is a fixed-length numeric string so sorting works correctly).
Step 2: Query using the composite field
Now you can filter for a specific user ID and sort by timestamp in one query:
String targetUserId = "user_123"; // Replace with your actual user ID Query currentUserSortedByTimestamp = FirebaseDatabase.getInstance().getReference() .child("Users") .orderByChild("currentUserId_timestamp") .startAt(targetUserId + "_") .endAt(targetUserId + "\uf8ff"); // \uf8ff is a high Unicode character to match all suffixes
Step 3: Add the index in Firebase Rules
Don't forget to define the index for this composite field in your Firebase Database Rules to ensure the query performs well:
{ "rules": { "Users": { ".indexOn": ["currentUserId_timestamp"] } } }
Option 2: Restructure your data (more efficient approach)
If you can adjust your database structure, grouping records by CurrentUserId makes this query way simpler and faster:
Users ├── user_123 │ ├── record1 │ │ ├── timestamp: 1699999999 │ │ └── ... other fields │ └── record2 │ ├── timestamp: 1700000000 │ └── ... other fields └── user_456 └── record3 ├── timestamp: 1700000001 └── ... other fields
Now you can directly access the user's node and sort by timestamp without any filtering hacks:
Query currentUserSortedByTimestamp = FirebaseDatabase.getInstance().getReference() .child("Users") .child("user_123") // Replace with your target user ID .orderByChild("timestamp");
Why your original code didn't work
When you call orderByChild twice:
Query Users = FirebaseDatabase.getInstance().getReference().child("Users").orderByChild("timestamp"); Query CurrentUser = Users.orderByChild("CurrentUserId");
The second orderByChild("CurrentUserId") replaces the first orderByChild("timestamp")—so your query ends up only sorting by CurrentUserId, not filtering or sorting by timestamp at all. Firebase doesn't support multiple orderBy calls in a single query, hence the need for the solutions above.
内容的提问来源于stack exchange,提问作者Vora

