[GH-ISSUE #7] Querying on children #9

Closed
opened 2026-05-23 08:25:12 -06:00 by gitea-mirror · 3 comments
Owner

Originally created by @paradis-A on GitHub (Feb 16, 2021).
Original GitHub issue: https://github.com/appy-one/acebase/issues/7

let say i have this collection.

//the collection sample
//parent collection here
stores = [
    {
        "some cuid": {
            "some props": "hello",
            // child collction
            products: [
                {
                    "some cuid": {
                        //some props
                    },
                },
            ],
        },
    },
]
//for example
db.ref("stores/some cuid").child('stores').push({
    // some props
})

how do i query products. i mean want to get all products.

Originally created by @paradis-A on GitHub (Feb 16, 2021). Original GitHub issue: https://github.com/appy-one/acebase/issues/7 let say i have this collection. ```javascript //the collection sample //parent collection here stores = [ { "some cuid": { "some props": "hello", // child collction products: [ { "some cuid": { //some props }, }, ], }, }, ] //for example db.ref("stores/some cuid").child('stores').push({ // some props }) ``` how do i query products. i mean want to get all products.
gitea-mirror 2026-05-23 08:25:12 -06:00
Author
Owner

@appy-one commented on GitHub (Feb 16, 2021):

Hi, I'm not really sure what you are trying to do here?
First of all, you are mixing arrays and object collections. Only use arrays for small collections of data that do not change frequently, use object collections for all other situations.

If you want to maintain a collection of products, do this:

db.ref('products').push({ title: 'product 1' }); // Add product to object collection
db.ref('products').push({ title: 'product 2' }); // Do it again

If you want all products you previously stored, simply get the value:

const productsSnapshot = await db.ref('products').get();

// Iterate all objects in the collection with snapshot.forEach:
productsSnapshot.forEach(productSnapshot => {
   const product = productSnapshot.val(); // { "title": "Product 1" }
});

// Or, do it yourself by getting .val()
const products = productsSnapshot.val(); // { "somecuid1": { "title": "Product 1" }, "somecuid2": { "title": "Product 2" } }

If you want to query (instead of getting all), use .query:

const querySnapshots = await db.ref('products')
.query()
.filter('title', '>', 'Product')
.get()

See documentation for more info.

<!-- gh-comment-id:779822536 --> @appy-one commented on GitHub (Feb 16, 2021): Hi, I'm not really sure what you are trying to do here? First of all, you are mixing arrays and object collections. Only use arrays for small collections of data that do not change frequently, use object collections for all other situations. If you want to maintain a collection of products, do this: ```js db.ref('products').push({ title: 'product 1' }); // Add product to object collection db.ref('products').push({ title: 'product 2' }); // Do it again ``` If you want all products you previously stored, simply get the value: ```js const productsSnapshot = await db.ref('products').get(); // Iterate all objects in the collection with snapshot.forEach: productsSnapshot.forEach(productSnapshot => { const product = productSnapshot.val(); // { "title": "Product 1" } }); // Or, do it yourself by getting .val() const products = productsSnapshot.val(); // { "somecuid1": { "title": "Product 1" }, "somecuid2": { "title": "Product 2" } } ``` If you want to query (instead of getting all), use ```.query```: ```js const querySnapshots = await db.ref('products') .query() .filter('title', '>', 'Product') .get() ``` See documentation for more info.
Author
Owner

@paradis-A commented on GitHub (Feb 16, 2021):

@appy-one Yay. I missed some part of the documentation. So its not suggested to store array on nested object if it's frequently updated. I thought I can query from an array inside a nested object. just like I imagined. I have to do the manual mappings after all. I should have put this to discussion tab maybe. Thanks for the fast reply anyway.

<!-- gh-comment-id:779832056 --> @paradis-A commented on GitHub (Feb 16, 2021): @appy-one Yay. I missed some part of the documentation. So its not suggested to store array on nested object if it's frequently updated. I thought I can query from an array inside a nested object. just like I imagined. I have to do the manual mappings after all. I should have put this to discussion tab maybe. Thanks for the fast reply anyway.
Author
Owner

@paradis-A commented on GitHub (Feb 16, 2021):

Are you going to implement after and before hooks on the acebase-server? just like on fastify

<!-- gh-comment-id:779833809 --> @paradis-A commented on GitHub (Feb 16, 2021): Are you going to implement after and before hooks on the acebase-server? just like on fastify
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#9
No description provided.