firebase / firebase/firebase-js-sdk

URL Index Creation: __name__ field is not sorted in the same direction of the last sorted field in the index definition

Open
#9,097 3 comments 0 reactions 2 assignees Claimed by @wu-hui View on GitHub
api: firestore needs-attention question Repro Needed
Dominant language
TypeScript
Stars
5.1k
Forks
1k
Avg merge
2d 21h
Merged PRs (30d)
37

Description

### Operating System

not applicable

### Environment (if applicable)

not applicable

### Firebase SDK Version

11.8.1

### Firebase SDK Product(s)

Firestore

### Project Tooling

doesnt matter but we use Webpack 5.98.0

### Detailed Problem Description

According to https://firebase.google.com/docs/firestore/query-data/index-overview :

> By default, the __name__ field is sorted in the same direction of the last sorted field in the index definition

however, if I run this query:

```ts
import { getFirestore, collection, query, where, orderBy, limit, getDocs } from "firebase/firestore";

const db = getFirestore();

const auditLogRef = collection(db, "Auditlog");
const q = query(
auditLogRef,
where("tenantID", "in", ["IeZfE0yKWs0kGBl1IG5M"]),
orderBy("_meta.dateCreated", "desc"),
orderBy('tenantID', 'asc'),
limit(50)
);

const querySnapshot = await getDocs(q);
querySnapshot.forEach(doc => {
console.log(doc.id, doc.data());
});
```

firebase gives me a link to create this index:

![Image](https://github.com/user-attachments/assets/3a2119e5-87d6-4f0a-b8e4-2acaf062770d)

Where `__name__` is not sorted according to the last field in the index, which would be `_meta.dateCreated:DESCENDING` but sorted `ASCENDING` for unknown reason.

Why is this? By what rules is this sorting determined? We need to create the indexes programatically and therefore need to know what index is required.

### Steps and code to reproduce issue

1. compose a query with a where-in+orderBy and orderBy instruction (on different field)
2. execute the query
3. click on the query create link
4. see that fierbase suggests __name__ beeing sorted not according to the last index field

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.