[GH-ISSUE #62] Filter on nested data #46

Closed
opened 2026-05-23 08:27:55 -06:00 by gitea-mirror · 1 comment
Owner

Originally created by @gryphonmyers on GitHub (Jan 30, 2022).
Original GitHub issue: https://github.com/appy-one/acebase/issues/62

After reading through the docs, it seems there may not be a way to filter on nested data. For example, let's say I have a collection of items that looks like:

const myEntries = [
  {id: 1, nestedObject: { color: "blue" } },
  {id: 2, nestedObject: { color: "pink" } }
]

Can I perform a query for only the first entry based on the fact that it has a blue nestedObject? Based on the docs that should look something like:

db.query('myEntries')
  .filter('nestedObject.color', 'like', 'blue') // not currently possible?

But I don't think this works because the first argument of filter is described as a "key" not an object notation path. Is there something I'm missing or is this really not possible? Are there plans to support this type of query?

Originally created by @gryphonmyers on GitHub (Jan 30, 2022). Original GitHub issue: https://github.com/appy-one/acebase/issues/62 After reading through the docs, it seems there may not be a way to filter on nested data. For example, let's say I have a collection of items that looks like: ```javascript const myEntries = [ {id: 1, nestedObject: { color: "blue" } }, {id: 2, nestedObject: { color: "pink" } } ] ``` Can I perform a query for only the first entry based on the fact that it has a `blue` nestedObject? Based on the docs that should look something like: ```javascript db.query('myEntries') .filter('nestedObject.color', 'like', 'blue') // not currently possible? ``` But I don't think this works because the first argument of filter is described as a "key" not an object notation path. Is there something I'm missing or is this really not possible? Are there plans to support this type of query?
Author
Owner

@appy-one commented on GitHub (Jan 30, 2022):

This is possible. Use filter('nestedObject/color', 'like', 'blue') instead. I see I didn't document that. Note that the collection in your example is an array, not a collection. See this example code:

const { AceBase } = require("acebase")
const db = new AceBase('test');

await db.ref('books').push({ title: 'book 1', cover: { color: 'blue' } });
await db.ref('books').push({ title: 'book 2', cover: { color: 'red' } });

const snaps = await db.query('books').filter('cover/color', '==', 'blue');

snaps.forEach(snap => console.log(snap.val()));
<!-- gh-comment-id:1025108395 --> @appy-one commented on GitHub (Jan 30, 2022): This is possible. Use `filter('nestedObject/color', 'like', 'blue')` instead. I see I didn't document that. Note that the collection in your example is an array, not a collection. See this example code: ```js const { AceBase } = require("acebase") const db = new AceBase('test'); await db.ref('books').push({ title: 'book 1', cover: { color: 'blue' } }); await db.ref('books').push({ title: 'book 2', cover: { color: 'red' } }); const snaps = await db.query('books').filter('cover/color', '==', 'blue'); snaps.forEach(snap => console.log(snap.val())); ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/acebase#46
No description provided.