$gt behaves like $gte when using sort with multiple fields
- Dominant language
- JavaScript
- Stars
- 17.6k
- Forks
- 1.5k
- PR merge metrics
- No merged PRs in 30d
Description
While migrating from CouchDB to PouchDB, I have encountered a potential bug in PouchDB where the behavior of `$gt` seems to change when a sort field is added to a find query. Without the sort field, `$gt` works as expected, but when a sort is applied, it behaves like `$gte`, returning additional documents.
Here is a minimal code example:
```javascript
const db = new PouchDB("users");
await db.createIndex({
index: { fields: ["age", "name"] },
});
await db.bulkDocs([
{
name: "Lisa",
age: 31,
},
{
name: "John",
age: 25,
},
]);
// Query without sorting
const { docs: docsWithoutSort } = await db.find({
selector: {
age: {
$gt: 25,
},
},
});
console.log("Without sorting:", docsWithoutSort.length); // ✅ Expected: 1
// Query with sorting
const { docs: docsWithSort } = await db.find({
selector: {
age: {
$gt: 25,
},
},
sort: [{ age: "desc" }, { name: "desc" }],
});
console.log("With sorting:", docsWithSort.length); // ❌ Expected: 1, Actual: 2
```
Contributor guide
Assessment
This issue has not been assessed yet.