2
votes

I need to store an "order" in Firestore. It's basically several rows of part#, description, quantity, and price. My initial thought is to store each order as a document where each line/row/lineItem is an array.

Considering search/scale, is this recommended? Or, should each line item of the "order" be a separate document of a top-level collection("order1")?

PART1A    a desc of part1a    3    13.95
PART2B    aanother descrip    7    9.99

UPDATE: Below is my solution. I spent (wasted) a long time thinking about how to .then update my db.collection("users").doc(uid).collection("orders"). After an hour I understood there no reason to do this at all. I've captured the uid in the db.collection("orders").doc() already and can quickly query that to pull a users orders up.

    ordersPageSaveButton.addEventListener('click', function(evt) {
        var batch = db.batch();
        var ordersRef = db.collection("orders").doc();
        var itemRows = parentEl.querySelectorAll(".selected-item-row");
        var name;
        var description;
        var quantity;
        var price;
        var indx;
        var obj = {};
        //order name would be nice
        obj.updated = Date.now();
        obj.owner = firebase.auth().currentUser.uid;
        obj.stage = "saved";
        for (var i = 0; i < itemRows.length; i++) {
            name = itemRows[i].querySelector(".product-container-item-name-wrapper").innerHTML.toString().toLowerCase();
            description = itemRows[i].querySelector(".product-container-description-row").innerHTML.toString().toLowerCase();
            quantity = Number(itemRows[i].querySelector(".product-container-qty-input").value);
            price = itemRows[i].querySelector(".product-container-unit-price-wrapper").innerHTML;
            price = parseFloat(price.replace(/,/g, ''));
            indx = itemRows[i].querySelector(".product-container-row-number-wrapper").innerHTML;
            obj[name] = {
            indx:indx,
            description:description,
            quantity:quantity,
            price:price};
            batch.set(ordersRef, obj);
        }
        // Commit the batch
        batch.commit().then(function () {
            console.log("success");
        }).catch(function(err) {
            console.error(err);
        });
    });
1
As is the general case for nosql databases, it depends entirely on how you intend to query your data. Without knowing your required queries, it's not really possible to say what's the best structure.Doug Stevenson
I would like user to be able to search for all orders including "red" in the description. Firestore Query Limitations is my concern there.Ronnie Royston
There's no full text searching in Firestore. You'll need to copy your data to another database for that. firebase.google.com/docs/firestore/solutions/searchDoug Stevenson
I think a map (object notation instead of [ ] notation) does support querying -> Working with Arrays, Lists, and Sets. Maybe this is the answer I'm looking for if anyone wants the points...Ronnie Royston
Sure, if you want to put every single word into that map, then assume that the word you're looking for hasn't been pluralized, changed tense, converted to an adjective or adverb, abbreviated, etc.Doug Stevenson

1 Answers

2
votes

It looks like the firtly mentioned structure is the way to go. Use the order name as the key (collection/$ordername) and description, quantity and price as values.

PART1A
    description: "This is desc of part1a"
    quantity: 3
    price: 13.95

This way you can order by any of the values and the name.