apache / apache/pouchdb

$gt behaves like $gte when using sort with multiple fields

Open
#9,056 1 comment 0 reactions 0 assignees View on GitHub
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

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.