appy-one / acebase

A fast, low memory, transactional, index & query enabled NoSQL database engine and server for node.js and browser with realtime data change notifications
MIT License
491 stars 27 forks source link

Filter on nested data #62

Closed gryphonmyers closed 2 years ago

gryphonmyers commented 2 years ago

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?

appy-one commented 2 years ago

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()));