Lets say I have an array field in a document:
entries: ["hello"]
I would like to append new elements to this array field i.e ["Yellow" ,"Blue"]
I did some research and most the techniques replace the entire record. How can I achieve this?
I'm using javascript.
Check out this question: How to append something to an array?
.
And looks like you are developing using firebase firestore, please take a look at their documentation here.
You'll have to retrieve the document you want to edit first, append the array, then update it.
Related
I want extract text values from every sub objects the given JSON structure . For do that I have used following JMSEPath query translations.en.*[0].[*].children.text but I was not unable to extract the value . Can someone suggest me a correct query or other approches
It may be better if you add to the question the expected output as well.
Making a big assumption that you want all text values "flatten up" into a single array you can try this: translation.en.*[].children[].text
Ref https://jmespath.org/tutorial.html#flatten-projections
I am using the REST Countries API to retrieve data from different countries, the point is to generate a HTML code to insert, depending on the country data.
But as I'm trying to point the language data in my HTML code, it of course returns me an object.
Here are two examples of the data from the API:
languages: {cat: 'Catalan'}
languages: {fra: 'French'}
The line of code in my HTML:
<p class="country__row"><span>🗣️</span>${data.languages}</p>
I would like to retrieve the values of the first field in those objects (Catalan or French) without having to, of course, write ${data.languages.fra} in my HTML code.
Thank you for your help.
I tried ${data.languages[0]} and ${data.languages[0].value}, doesn't work.
You can use the Object.keys() function to get the list of keys from an object.
There are other similar functions as well that you can use such as Object.values() or Object.entries().
As far as your use case is concerned try any one of the solutions mentioned below:
${data.languages[Object.keys(data.languages)[0]]}
// The `0` index can be replaced with i if using a loop.
${Object.values(data.languages)[0]}
You can write as Object.values(data.languages) which would give you values of object.
I have a firestore firebase database , in which I have a collection users
there is an array in the collection and in the array there is a map
in map there is a field qty.. I want to increment that qty value..
using increment doesnt help as the qty is inside a array index
db.collection("users").doc(checkId).update({
myCart: firebase.firestore.FieldValue.arrayUnion({
qty: firebase.firestore.FieldValue.increment(1),
}),
this is the error Output =>
Uncaught (in promise) FirebaseError: Function FieldValue.arrayUnion() called with invalid data. FieldValue.increment() can only be used with update() and set()
My answer below won't work, given that the qty is in an array. The only way to update an item in an array is to read the entire document, update the item in the array, and then write the entire array with the updated item back to the document.
An alternative would be to use a map instead of an array, and then update the qty using the approach outlined in my (old, and non-working) answer below 👇
You need to specify the full path to the field you're trying to update. So I think in your case, that'll be:
db.collection("users").doc(checkId).update({
"myCart.0.qty": firebase.firestore.FieldValue.increment(1)
}),
The field you want to update is embedded in an array. In this case, you can't use FieldValue.increment(), since it's not possible to call out an array element as a named field value.
What you'll have to do instead is read the entire document, modify the field in memory to contain what you want, and update the field back into the document. Also consider using a transaction for this if you need to update to be atomic.
(If the field wasn't part of an array, you could use FieldValue.increment().)
As of today (29-04-2020)... this is tested by me.
Suppose my data structure is like this:
collection: Users
Any document: say jdfhjksdhfw
It has a map like below
map name: UserPageVisits
map fields: field1,field2,field3 etc
Now we can increment the number field in the map like below:
mapname.field1 etc...
That is use the dot operator to access the fields inside the map just like you would do to an object of javascript.
JAVA Code (Android), update the field using transactions so they can complete atomically.
transaction.update(<documentreference object>,"UserPageVisits.field1",FieldValue.increment(1));
I have just pushed a version of my app which uses this concept and it's working.
Kudos !!
My Best Regards
Previous answers helped me as well, but dont forget about the "merge" property!!! Otherwise it will overwrite your entire array, losing other fields.
var myIndex = 0;
const userRef = db.collection('users').doc(checkId);
return userRef.update({
'myCart.${myIndex}.qty': admin.firestore.FieldValue.increment(1)
}, {
merge: true
});
I'm using Firebase and Vuejs to create an database element, which has object array inside.
That's how the field looks, and I want to add tasks through the form into the 'moreTasks' as an array.
I tried using this, but it just creates new entity in the database.
db.collection('Tasks').add({
tasker: this.tasker.taskerName
})
I also tried checking API but I couldnt understand the refs, because I was using different methods to achieve that goal.
creatTask() {
db.collection('Tasks').add({
task_id: this.task_id,
name: this.name,
What would be correct way to approach this problem?
You can append an item to an array using FieldValue.arrayUnion() as described in the documentation. For example:
// Atomically add a new region to the "regions" array field.
washingtonRef.update({
regions: firebase.firestore.FieldValue.arrayUnion("greater_virginia")
});
The accepted answer used to be correct but is now wrong. Now there is an atomic append operation using the arrayUnion method:
https://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array
This is true as long as you are using firestore and not real time db (which I assume is the case from the tags)
I have a couple of tumblr-posts that I wish to output using mustache.
In tumblr, posts come in an array with different sizes. I always want to use alt_sizes[2]
I don't know if it's possible to refence arrays by id. My current solution is to pick it out in advance like this:
height.photos[z].columnPhoto = post.photos[z].alt_sizes[2]
and later in my template I can target the data like this:
{{#photos}}
{{columnPhoto.url}}
{{/photos}}
Phtotos is also an array I'm looping through
Is it possible to target alt_sizes[2] straight from the template?
You need to use the "dot"-annotation, like this:
{{#photos}}
{{alt_sizes.2.url}}
{{/photos}}