how to maintain duplicate data between documents sync on firebase?
I have a question about firestore and cloud function.
let say i have 2 collection:
products and reuse_shopping_carts.
and my reuse_shopping_cart document contain many products with all it’s properties (id,name,price,href).
now i want that if i change the price of a specific product in products collection , a cloud function will run on all reuse_shopping_cart documents and in every document that contains the specific product it will change is price.
someone can explain me how?
or what is the right approach to do it?
i know i can store in the reuse_shopping_cart only the product id and fetch the updated product data at client, but for a list of 100 product in one reuse_shopping_cart it’s will require 100 separate query for every time i want to read the shopping list, and changing price of a product is mach more rare, so i think it’s a better approach.
if you think differently i will happy to hear...
thank you!
firebase google-cloud-firestore google-cloud-functions
add a comment |
I have a question about firestore and cloud function.
let say i have 2 collection:
products and reuse_shopping_carts.
and my reuse_shopping_cart document contain many products with all it’s properties (id,name,price,href).
now i want that if i change the price of a specific product in products collection , a cloud function will run on all reuse_shopping_cart documents and in every document that contains the specific product it will change is price.
someone can explain me how?
or what is the right approach to do it?
i know i can store in the reuse_shopping_cart only the product id and fetch the updated product data at client, but for a list of 100 product in one reuse_shopping_cart it’s will require 100 separate query for every time i want to read the shopping list, and changing price of a product is mach more rare, so i think it’s a better approach.
if you think differently i will happy to hear...
thank you!
firebase google-cloud-firestore google-cloud-functions
If price changes are less common than reads of shopping carts, then your duplication of the price into the shopping cart will save you on IO operations. In fact, I'm not even sure you should update the price in all shopping carts, since the price may have been determined when the user added the item to the cart. For all options, see my answer here: stackoverflow.com/questions/30693785/…
– Frank van Puffelen
Jan 3 at 14:22
Hi Frank, in normal shopping cart you right, but this is reuse shopping cart that stay forever for the user and it's a start point for a new shopping cart so the price need to be changeable over time. i read about "Transactions and batched writes" in the firebase doc but it's limited to a maximum of 500 documents... so it's a problem because after some time i will have more the 500 reuse shopping list. any advice?
– Haim Cohen
Jan 3 at 15:40
Ah OK. In that case it makes sense. The options in my linked answer remain the same, and your approach of updating them from Cloud Functions sounds valid to me.
– Frank van Puffelen
Jan 3 at 15:43
add a comment |
I have a question about firestore and cloud function.
let say i have 2 collection:
products and reuse_shopping_carts.
and my reuse_shopping_cart document contain many products with all it’s properties (id,name,price,href).
now i want that if i change the price of a specific product in products collection , a cloud function will run on all reuse_shopping_cart documents and in every document that contains the specific product it will change is price.
someone can explain me how?
or what is the right approach to do it?
i know i can store in the reuse_shopping_cart only the product id and fetch the updated product data at client, but for a list of 100 product in one reuse_shopping_cart it’s will require 100 separate query for every time i want to read the shopping list, and changing price of a product is mach more rare, so i think it’s a better approach.
if you think differently i will happy to hear...
thank you!
firebase google-cloud-firestore google-cloud-functions
I have a question about firestore and cloud function.
let say i have 2 collection:
products and reuse_shopping_carts.
and my reuse_shopping_cart document contain many products with all it’s properties (id,name,price,href).
now i want that if i change the price of a specific product in products collection , a cloud function will run on all reuse_shopping_cart documents and in every document that contains the specific product it will change is price.
someone can explain me how?
or what is the right approach to do it?
i know i can store in the reuse_shopping_cart only the product id and fetch the updated product data at client, but for a list of 100 product in one reuse_shopping_cart it’s will require 100 separate query for every time i want to read the shopping list, and changing price of a product is mach more rare, so i think it’s a better approach.
if you think differently i will happy to hear...
thank you!
firebase google-cloud-firestore google-cloud-functions
firebase google-cloud-firestore google-cloud-functions
edited Jan 3 at 11:34
Haim Cohen
asked Jan 3 at 11:11
Haim CohenHaim Cohen
13
13
If price changes are less common than reads of shopping carts, then your duplication of the price into the shopping cart will save you on IO operations. In fact, I'm not even sure you should update the price in all shopping carts, since the price may have been determined when the user added the item to the cart. For all options, see my answer here: stackoverflow.com/questions/30693785/…
– Frank van Puffelen
Jan 3 at 14:22
Hi Frank, in normal shopping cart you right, but this is reuse shopping cart that stay forever for the user and it's a start point for a new shopping cart so the price need to be changeable over time. i read about "Transactions and batched writes" in the firebase doc but it's limited to a maximum of 500 documents... so it's a problem because after some time i will have more the 500 reuse shopping list. any advice?
– Haim Cohen
Jan 3 at 15:40
Ah OK. In that case it makes sense. The options in my linked answer remain the same, and your approach of updating them from Cloud Functions sounds valid to me.
– Frank van Puffelen
Jan 3 at 15:43
add a comment |
If price changes are less common than reads of shopping carts, then your duplication of the price into the shopping cart will save you on IO operations. In fact, I'm not even sure you should update the price in all shopping carts, since the price may have been determined when the user added the item to the cart. For all options, see my answer here: stackoverflow.com/questions/30693785/…
– Frank van Puffelen
Jan 3 at 14:22
Hi Frank, in normal shopping cart you right, but this is reuse shopping cart that stay forever for the user and it's a start point for a new shopping cart so the price need to be changeable over time. i read about "Transactions and batched writes" in the firebase doc but it's limited to a maximum of 500 documents... so it's a problem because after some time i will have more the 500 reuse shopping list. any advice?
– Haim Cohen
Jan 3 at 15:40
Ah OK. In that case it makes sense. The options in my linked answer remain the same, and your approach of updating them from Cloud Functions sounds valid to me.
– Frank van Puffelen
Jan 3 at 15:43
If price changes are less common than reads of shopping carts, then your duplication of the price into the shopping cart will save you on IO operations. In fact, I'm not even sure you should update the price in all shopping carts, since the price may have been determined when the user added the item to the cart. For all options, see my answer here: stackoverflow.com/questions/30693785/…
– Frank van Puffelen
Jan 3 at 14:22
If price changes are less common than reads of shopping carts, then your duplication of the price into the shopping cart will save you on IO operations. In fact, I'm not even sure you should update the price in all shopping carts, since the price may have been determined when the user added the item to the cart. For all options, see my answer here: stackoverflow.com/questions/30693785/…
– Frank van Puffelen
Jan 3 at 14:22
Hi Frank, in normal shopping cart you right, but this is reuse shopping cart that stay forever for the user and it's a start point for a new shopping cart so the price need to be changeable over time. i read about "Transactions and batched writes" in the firebase doc but it's limited to a maximum of 500 documents... so it's a problem because after some time i will have more the 500 reuse shopping list. any advice?
– Haim Cohen
Jan 3 at 15:40
Hi Frank, in normal shopping cart you right, but this is reuse shopping cart that stay forever for the user and it's a start point for a new shopping cart so the price need to be changeable over time. i read about "Transactions and batched writes" in the firebase doc but it's limited to a maximum of 500 documents... so it's a problem because after some time i will have more the 500 reuse shopping list. any advice?
– Haim Cohen
Jan 3 at 15:40
Ah OK. In that case it makes sense. The options in my linked answer remain the same, and your approach of updating them from Cloud Functions sounds valid to me.
– Frank van Puffelen
Jan 3 at 15:43
Ah OK. In that case it makes sense. The options in my linked answer remain the same, and your approach of updating them from Cloud Functions sounds valid to me.
– Frank van Puffelen
Jan 3 at 15:43
add a comment |
1 Answer
1
active
oldest
votes
You can structure your reuse_shopping_carts document like this:
products: (map)
(product id)
name:
value:
For example:

Then to find which documents to update when the price changes you can query like this:
firebase.firestore().collection('reuse_shopping_carts').where(`products.${productId}.price`, '>=', 0)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54021169%2fhow-to-maintain-duplicate-data-between-documents-sync-on-firebase%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can structure your reuse_shopping_carts document like this:
products: (map)
(product id)
name:
value:
For example:

Then to find which documents to update when the price changes you can query like this:
firebase.firestore().collection('reuse_shopping_carts').where(`products.${productId}.price`, '>=', 0)
add a comment |
You can structure your reuse_shopping_carts document like this:
products: (map)
(product id)
name:
value:
For example:

Then to find which documents to update when the price changes you can query like this:
firebase.firestore().collection('reuse_shopping_carts').where(`products.${productId}.price`, '>=', 0)
add a comment |
You can structure your reuse_shopping_carts document like this:
products: (map)
(product id)
name:
value:
For example:

Then to find which documents to update when the price changes you can query like this:
firebase.firestore().collection('reuse_shopping_carts').where(`products.${productId}.price`, '>=', 0)
You can structure your reuse_shopping_carts document like this:
products: (map)
(product id)
name:
value:
For example:

Then to find which documents to update when the price changes you can query like this:
firebase.firestore().collection('reuse_shopping_carts').where(`products.${productId}.price`, '>=', 0)
answered Jan 3 at 12:03
Ricardo SmaniaRicardo Smania
1,1581122
1,1581122
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54021169%2fhow-to-maintain-duplicate-data-between-documents-sync-on-firebase%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If price changes are less common than reads of shopping carts, then your duplication of the price into the shopping cart will save you on IO operations. In fact, I'm not even sure you should update the price in all shopping carts, since the price may have been determined when the user added the item to the cart. For all options, see my answer here: stackoverflow.com/questions/30693785/…
– Frank van Puffelen
Jan 3 at 14:22
Hi Frank, in normal shopping cart you right, but this is reuse shopping cart that stay forever for the user and it's a start point for a new shopping cart so the price need to be changeable over time. i read about "Transactions and batched writes" in the firebase doc but it's limited to a maximum of 500 documents... so it's a problem because after some time i will have more the 500 reuse shopping list. any advice?
– Haim Cohen
Jan 3 at 15:40
Ah OK. In that case it makes sense. The options in my linked answer remain the same, and your approach of updating them from Cloud Functions sounds valid to me.
– Frank van Puffelen
Jan 3 at 15:43