Firestore update specific element in array
Im using Google Firestore with Angular and I'm trying to update a specific element inside an array.
I've tried a lot of things but with no succsess at all.
How can I reach to the specific company index inside the companies array?
Thanks :)
angular firebase google-cloud-firestore
add a comment |
Im using Google Firestore with Angular and I'm trying to update a specific element inside an array.
I've tried a lot of things but with no succsess at all.
How can I reach to the specific company index inside the companies array?
Thanks :)
angular firebase google-cloud-firestore
"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example."
– Frank van Puffelen
Nov 15 '18 at 14:09
add a comment |
Im using Google Firestore with Angular and I'm trying to update a specific element inside an array.
I've tried a lot of things but with no succsess at all.
How can I reach to the specific company index inside the companies array?
Thanks :)
angular firebase google-cloud-firestore
Im using Google Firestore with Angular and I'm trying to update a specific element inside an array.
I've tried a lot of things but with no succsess at all.
How can I reach to the specific company index inside the companies array?
Thanks :)
angular firebase google-cloud-firestore
angular firebase google-cloud-firestore
edited Nov 15 '18 at 14:01
Alex Mamo
45k82863
45k82863
asked Nov 15 '18 at 12:28
NaurutoNauruto
689
689
"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example."
– Frank van Puffelen
Nov 15 '18 at 14:09
add a comment |
"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example."
– Frank van Puffelen
Nov 15 '18 at 14:09
"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example."
– Frank van Puffelen
Nov 15 '18 at 14:09
"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example."
– Frank van Puffelen
Nov 15 '18 at 14:09
add a comment |
2 Answers
2
active
oldest
votes
How can I reach to the specific company index inside the companies array?
There is no way you can get, update or remove an element in a Firestore array according with the corresponding index. Try to think what might happen if a user wants to edit a value at index 0, some other user wants to delete the value at index 0 and in the same time another user wants to add another value at index 1, you'll end up having very different results and why not, an ArrayIndexOutOfBoundsException. So Firestore actions with arrays are a little bit different. So you cannot perform actions like, insert, update or delete at a specific index. So use arrays only if don't care about the exact order that you store elements.
Firestore added a few weeks ago some features to add or remove specific elements but only if don't care about the exact position of them. See here official documentation.
In your case, you don't have a simple array of strings you have an array of objects, which means that in order to do some chnages, you should get the entire object, iterate over the properties, make the necessary changes and write it back to the database.
Ok. Actually I dont care about the exact position. I just want to edit an elemet and write it back to the array in the database. Any ideas of how to do it?
– Nauruto
Nov 15 '18 at 13:30
If you don't care about the position, as you initial have asked then you just should make your own attempt given the information in the answer and in the offical documentation regarding adding data and ask another question if something else comes up.
– Alex Mamo
Nov 15 '18 at 13:42
I edited my question my friend. For now I manage to do it by deleting the specific object from the array and insert the updated one after. Is there any other way to do it?
– Nauruto
Nov 15 '18 at 14:00
1
I rolled back your question. Don't edit your actual question to add another one. Let's stick to one question at a time. If you have another problem after this one is explained, post a new question for that problem with its own MCVE.
– Alex Mamo
Nov 15 '18 at 14:06
So in order to follow the rules of this comunity, please post another fresh question, so me and other users can help you.
– Alex Mamo
Nov 15 '18 at 14:06
|
show 1 more comment
Ok there is a way to query an array.
I'm not 100% if this is going to work but I tried it before. It might need the complete object.
this.usersCollection = this.afs.collection('Users', ref => ref.where('companies', 'array-contains', {location: 'test'}) );
If this not work you always have the option to make a subcollection with the companies inside there as a doc.
The OP didn't ask how to query an array; they asked how to access a specific, known element within an array, which would eliminate a query since the element is known.
– Jay
Nov 15 '18 at 19:03
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%2f53319529%2ffirestore-update-specific-element-in-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
How can I reach to the specific company index inside the companies array?
There is no way you can get, update or remove an element in a Firestore array according with the corresponding index. Try to think what might happen if a user wants to edit a value at index 0, some other user wants to delete the value at index 0 and in the same time another user wants to add another value at index 1, you'll end up having very different results and why not, an ArrayIndexOutOfBoundsException. So Firestore actions with arrays are a little bit different. So you cannot perform actions like, insert, update or delete at a specific index. So use arrays only if don't care about the exact order that you store elements.
Firestore added a few weeks ago some features to add or remove specific elements but only if don't care about the exact position of them. See here official documentation.
In your case, you don't have a simple array of strings you have an array of objects, which means that in order to do some chnages, you should get the entire object, iterate over the properties, make the necessary changes and write it back to the database.
Ok. Actually I dont care about the exact position. I just want to edit an elemet and write it back to the array in the database. Any ideas of how to do it?
– Nauruto
Nov 15 '18 at 13:30
If you don't care about the position, as you initial have asked then you just should make your own attempt given the information in the answer and in the offical documentation regarding adding data and ask another question if something else comes up.
– Alex Mamo
Nov 15 '18 at 13:42
I edited my question my friend. For now I manage to do it by deleting the specific object from the array and insert the updated one after. Is there any other way to do it?
– Nauruto
Nov 15 '18 at 14:00
1
I rolled back your question. Don't edit your actual question to add another one. Let's stick to one question at a time. If you have another problem after this one is explained, post a new question for that problem with its own MCVE.
– Alex Mamo
Nov 15 '18 at 14:06
So in order to follow the rules of this comunity, please post another fresh question, so me and other users can help you.
– Alex Mamo
Nov 15 '18 at 14:06
|
show 1 more comment
How can I reach to the specific company index inside the companies array?
There is no way you can get, update or remove an element in a Firestore array according with the corresponding index. Try to think what might happen if a user wants to edit a value at index 0, some other user wants to delete the value at index 0 and in the same time another user wants to add another value at index 1, you'll end up having very different results and why not, an ArrayIndexOutOfBoundsException. So Firestore actions with arrays are a little bit different. So you cannot perform actions like, insert, update or delete at a specific index. So use arrays only if don't care about the exact order that you store elements.
Firestore added a few weeks ago some features to add or remove specific elements but only if don't care about the exact position of them. See here official documentation.
In your case, you don't have a simple array of strings you have an array of objects, which means that in order to do some chnages, you should get the entire object, iterate over the properties, make the necessary changes and write it back to the database.
Ok. Actually I dont care about the exact position. I just want to edit an elemet and write it back to the array in the database. Any ideas of how to do it?
– Nauruto
Nov 15 '18 at 13:30
If you don't care about the position, as you initial have asked then you just should make your own attempt given the information in the answer and in the offical documentation regarding adding data and ask another question if something else comes up.
– Alex Mamo
Nov 15 '18 at 13:42
I edited my question my friend. For now I manage to do it by deleting the specific object from the array and insert the updated one after. Is there any other way to do it?
– Nauruto
Nov 15 '18 at 14:00
1
I rolled back your question. Don't edit your actual question to add another one. Let's stick to one question at a time. If you have another problem after this one is explained, post a new question for that problem with its own MCVE.
– Alex Mamo
Nov 15 '18 at 14:06
So in order to follow the rules of this comunity, please post another fresh question, so me and other users can help you.
– Alex Mamo
Nov 15 '18 at 14:06
|
show 1 more comment
How can I reach to the specific company index inside the companies array?
There is no way you can get, update or remove an element in a Firestore array according with the corresponding index. Try to think what might happen if a user wants to edit a value at index 0, some other user wants to delete the value at index 0 and in the same time another user wants to add another value at index 1, you'll end up having very different results and why not, an ArrayIndexOutOfBoundsException. So Firestore actions with arrays are a little bit different. So you cannot perform actions like, insert, update or delete at a specific index. So use arrays only if don't care about the exact order that you store elements.
Firestore added a few weeks ago some features to add or remove specific elements but only if don't care about the exact position of them. See here official documentation.
In your case, you don't have a simple array of strings you have an array of objects, which means that in order to do some chnages, you should get the entire object, iterate over the properties, make the necessary changes and write it back to the database.
How can I reach to the specific company index inside the companies array?
There is no way you can get, update or remove an element in a Firestore array according with the corresponding index. Try to think what might happen if a user wants to edit a value at index 0, some other user wants to delete the value at index 0 and in the same time another user wants to add another value at index 1, you'll end up having very different results and why not, an ArrayIndexOutOfBoundsException. So Firestore actions with arrays are a little bit different. So you cannot perform actions like, insert, update or delete at a specific index. So use arrays only if don't care about the exact order that you store elements.
Firestore added a few weeks ago some features to add or remove specific elements but only if don't care about the exact position of them. See here official documentation.
In your case, you don't have a simple array of strings you have an array of objects, which means that in order to do some chnages, you should get the entire object, iterate over the properties, make the necessary changes and write it back to the database.
answered Nov 15 '18 at 12:41
Alex MamoAlex Mamo
45k82863
45k82863
Ok. Actually I dont care about the exact position. I just want to edit an elemet and write it back to the array in the database. Any ideas of how to do it?
– Nauruto
Nov 15 '18 at 13:30
If you don't care about the position, as you initial have asked then you just should make your own attempt given the information in the answer and in the offical documentation regarding adding data and ask another question if something else comes up.
– Alex Mamo
Nov 15 '18 at 13:42
I edited my question my friend. For now I manage to do it by deleting the specific object from the array and insert the updated one after. Is there any other way to do it?
– Nauruto
Nov 15 '18 at 14:00
1
I rolled back your question. Don't edit your actual question to add another one. Let's stick to one question at a time. If you have another problem after this one is explained, post a new question for that problem with its own MCVE.
– Alex Mamo
Nov 15 '18 at 14:06
So in order to follow the rules of this comunity, please post another fresh question, so me and other users can help you.
– Alex Mamo
Nov 15 '18 at 14:06
|
show 1 more comment
Ok. Actually I dont care about the exact position. I just want to edit an elemet and write it back to the array in the database. Any ideas of how to do it?
– Nauruto
Nov 15 '18 at 13:30
If you don't care about the position, as you initial have asked then you just should make your own attempt given the information in the answer and in the offical documentation regarding adding data and ask another question if something else comes up.
– Alex Mamo
Nov 15 '18 at 13:42
I edited my question my friend. For now I manage to do it by deleting the specific object from the array and insert the updated one after. Is there any other way to do it?
– Nauruto
Nov 15 '18 at 14:00
1
I rolled back your question. Don't edit your actual question to add another one. Let's stick to one question at a time. If you have another problem after this one is explained, post a new question for that problem with its own MCVE.
– Alex Mamo
Nov 15 '18 at 14:06
So in order to follow the rules of this comunity, please post another fresh question, so me and other users can help you.
– Alex Mamo
Nov 15 '18 at 14:06
Ok. Actually I dont care about the exact position. I just want to edit an elemet and write it back to the array in the database. Any ideas of how to do it?
– Nauruto
Nov 15 '18 at 13:30
Ok. Actually I dont care about the exact position. I just want to edit an elemet and write it back to the array in the database. Any ideas of how to do it?
– Nauruto
Nov 15 '18 at 13:30
If you don't care about the position, as you initial have asked then you just should make your own attempt given the information in the answer and in the offical documentation regarding adding data and ask another question if something else comes up.
– Alex Mamo
Nov 15 '18 at 13:42
If you don't care about the position, as you initial have asked then you just should make your own attempt given the information in the answer and in the offical documentation regarding adding data and ask another question if something else comes up.
– Alex Mamo
Nov 15 '18 at 13:42
I edited my question my friend. For now I manage to do it by deleting the specific object from the array and insert the updated one after. Is there any other way to do it?
– Nauruto
Nov 15 '18 at 14:00
I edited my question my friend. For now I manage to do it by deleting the specific object from the array and insert the updated one after. Is there any other way to do it?
– Nauruto
Nov 15 '18 at 14:00
1
1
I rolled back your question. Don't edit your actual question to add another one. Let's stick to one question at a time. If you have another problem after this one is explained, post a new question for that problem with its own MCVE.
– Alex Mamo
Nov 15 '18 at 14:06
I rolled back your question. Don't edit your actual question to add another one. Let's stick to one question at a time. If you have another problem after this one is explained, post a new question for that problem with its own MCVE.
– Alex Mamo
Nov 15 '18 at 14:06
So in order to follow the rules of this comunity, please post another fresh question, so me and other users can help you.
– Alex Mamo
Nov 15 '18 at 14:06
So in order to follow the rules of this comunity, please post another fresh question, so me and other users can help you.
– Alex Mamo
Nov 15 '18 at 14:06
|
show 1 more comment
Ok there is a way to query an array.
I'm not 100% if this is going to work but I tried it before. It might need the complete object.
this.usersCollection = this.afs.collection('Users', ref => ref.where('companies', 'array-contains', {location: 'test'}) );
If this not work you always have the option to make a subcollection with the companies inside there as a doc.
The OP didn't ask how to query an array; they asked how to access a specific, known element within an array, which would eliminate a query since the element is known.
– Jay
Nov 15 '18 at 19:03
add a comment |
Ok there is a way to query an array.
I'm not 100% if this is going to work but I tried it before. It might need the complete object.
this.usersCollection = this.afs.collection('Users', ref => ref.where('companies', 'array-contains', {location: 'test'}) );
If this not work you always have the option to make a subcollection with the companies inside there as a doc.
The OP didn't ask how to query an array; they asked how to access a specific, known element within an array, which would eliminate a query since the element is known.
– Jay
Nov 15 '18 at 19:03
add a comment |
Ok there is a way to query an array.
I'm not 100% if this is going to work but I tried it before. It might need the complete object.
this.usersCollection = this.afs.collection('Users', ref => ref.where('companies', 'array-contains', {location: 'test'}) );
If this not work you always have the option to make a subcollection with the companies inside there as a doc.
Ok there is a way to query an array.
I'm not 100% if this is going to work but I tried it before. It might need the complete object.
this.usersCollection = this.afs.collection('Users', ref => ref.where('companies', 'array-contains', {location: 'test'}) );
If this not work you always have the option to make a subcollection with the companies inside there as a doc.
edited Nov 15 '18 at 15:13
answered Nov 15 '18 at 15:06
SwooxSwoox
1,7931621
1,7931621
The OP didn't ask how to query an array; they asked how to access a specific, known element within an array, which would eliminate a query since the element is known.
– Jay
Nov 15 '18 at 19:03
add a comment |
The OP didn't ask how to query an array; they asked how to access a specific, known element within an array, which would eliminate a query since the element is known.
– Jay
Nov 15 '18 at 19:03
The OP didn't ask how to query an array; they asked how to access a specific, known element within an array, which would eliminate a query since the element is known.
– Jay
Nov 15 '18 at 19:03
The OP didn't ask how to query an array; they asked how to access a specific, known element within an array, which would eliminate a query since the element is known.
– Jay
Nov 15 '18 at 19:03
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%2f53319529%2ffirestore-update-specific-element-in-array%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
"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example."
– Frank van Puffelen
Nov 15 '18 at 14:09