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);
});
});
答案 0 :(得分:1)
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.