3

I'm new with Firestore and I'm trying to migrate a MongoDB project to Firestore. I have the following issue. My data structure is this one:

/products
    'A': {name: 'Product A', price: 1},
    'B': {name: 'Product B', price: 2}

/profiles
    '1': {name: 'John', purchases: [ {product: 'A', quantity: 1} ] }

I have a collection of products and a collection of profiles. These profiles have an array of purchases, and each purchase has a object reference to the corresponding product and a quantity. With MongoDb I could do this:

const profiles = await Profiles.findById('1').populate('purchases.product', 'name price');

So I could get the following object:

{
    _id: '1',
    name: 'John',
    purchases: [
        {
            {
                _id: 'A',
                name: 'Product A',
                price: 1,
            },
            quantity: 1,
        }
    ]
}

So with one query I can get all the purchases, I don't have to iterate through all purchases and get them by id. How can I achieve the same performance with Firestore? Also if this data model doesn't work as well with Firestore please tell me.

Guim
  • 628
  • 2
  • 12
  • 26
  • Here are some of my suggestions on another question: https://stackoverflow.com/questions/60255145/how-to-avoid-n-1-queries-in-firestore/65554546#65554546 – Nigel Sheridan-Smith Jan 03 '21 at 20:31

2 Answers2

6

What you're looking for is some kind of "inner join", which is not natively supported by firestore. You would have to query the docs, wait for firestore to return the values, iterate over the results to query the embedded docs, which is extremely tedious and prone to many errors. Firestore is an extremely good choice to go by, depending on your data structure but you will unfortunately suffer heavily in terms of queries, especially moving from a rich db like mongo.

Sidenote My company migrated some data from firestore to mongodb Atlas because of this problem, and I would highly recommend Mongodb Atlas and Sticth which provided features similar to firestore while maintaining the versatility of mongo. If it makes a difference, it also has (Though in Beta at the time of writing) GraphQl support.

Conclusion: No native support for joins. However, you can use clever rxjs and hack this problem, specifically CombineLatest. Heres a good answer to get you started

Kisinga
  • 1,640
  • 18
  • 27
  • I really dont get it... neither does it support inner joins query nor supports multiple inequality where clauses nor group by .... what is the good about firestore over mongodb or any other RDBMS? – vins Jun 30 '22 at 07:04
  • it's simplicity is a huge selling point plus not all apps need that level of complexity – Kisinga Jul 11 '22 at 11:40
2

With Firestore, the only ways to add documents involve either adding a single document at a time (in a loop), or using a batch write (in a loop with up to 500 documents per batch write). There are no other options provided by the client APIs.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441