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)
Related
So I'm working on a personal project to learn react-native and Firestore.
I have a DB like this:
And I want my code to add a new battery in the array batteries.
The elements in the array are just a map{string, string}
The problem is that when I update the array with a new brand that's work but if I want to update it with the same brand again have,
so having by the end
batteries[0]: {'brand': 'cestmoi'}
batteries[1]: {'brand': 'cestmoi'}
The DB doesn't update, doesn't have any error or so.
I don't understand why and I followed their tutorial. Here is my code:
async function addData(collection, doc, value) {
console.log(`Add data ${value.brand}`)
try {
const result = await firestore()
.collection(collection)
.doc(doc)
.set({
batteries: firestore.FieldValue.arrayUnion(value)
})
console.log(result);
return result;
} catch (error) {
return error;
}
}
I use try-catch by habit but I don't know if the then...catch is better or not.
As already #windowsill mentioned in his answer, there is no way you can add duplicate elements in an array using client-side code. If your application requires that, then you have to read the entire array, add the duplicates and then write the document back to Firestore.
However, if you want to update an existing element in an array of objects (maps) then you have to use arrayUnion with the entire object. If you want to understand the mechanism better, you can read the following article which is called:
How to update an array of objects in Firestore?
arrayUnion says that it "adds elements to an array but only elements not already present". Maybe it does a stringify or something to check equality and therefore doesn't add the new element. I think you'll have to 1. get the current list, 2. add your element, 3. set the batteries field to the updated list.
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 trying to save some data in firestore, the data consists of events, each event has a date and each some attendees.
What I'm trying to do now is model it like this events/${eventDate}/${userEmail} and then I would set this with the user's data. However when I try to set this data I get an error saying that the segment number should be even.
When I added another segment in the path (which I didn't want to do):
events/${eventDate}/attendees/${userEmail} I was able to set the data but I wasn't able to retrieve it (trying to retrieve all attendees of a given event date.
// insertion - this worked after some tweaking
this.db.collection('pickups').doc(pickupDate).set({ [email]: userData})
// deletion (this doesn't work - expects even number of segments)
this.db.collection('pickups').doc(`${pickupDate}/${email}`).delete()
// retrieval (works)
this.db.collection('pickups').doc(pickupDate).valueChanges()
Current delete:
this.db.collection('pickups').doc(pickupDate).update({
[email]: firestore.FieldValue.delete()
})
What am I missing here? Isn't this supposed to be like regular JSON?
The path you're currently trying events/${eventDate}/${userEmail} is interpreted as a collection (events) then a document (eventDate) then another document (userEmail).
What you actually have is a collection, document within that collection, field within that document.
It looks like you're adding the email correctly (I would remove the brackets around the word email though), but trying to delete incorrectly. You delete fields like this:
var removeCapital = cityRef.update({
capital: firebase.firestore.FieldValue.delete()
});
You can see the documentation here: https://firebase.google.com/docs/firestore/manage-data/delete-data#fields
The delete may look like this:
this.db.collection('pickups').doc(pickupDate).update({
email: firebase.firestore.FieldValue.delete()
})
It sounds like what you're trying to do is delete a field out of a document. However, this code you have:
this.db.collection('pickups').doc(`${pickupDate}/${email}`).delete()
is trying to build a reference to a collection, then delete it. It's not correct to use collection() and doc() to reference fields in a document. They are just used to build references to documents and collections.
If you want to delete a field in a document, first build a reference to the document that contains the field:
const ref = this.db.collection('pickups').doc(pickupDate)
Then update the document to indicate that you want the field removed:
ref.update({ [email]: firebase.firestore.FieldValue.delete() }}
The way you reference delete() out of FieldValue is going to change based on how you have the SDK imported into your code.
See the documentation on deleting fields for more information.
I've been going through the docs, but I don't see a way to update a value at a specific key (while keeping the rest). Is the only way to achieve this is to fetch the existing data and overwrite it using set()?
I have something like this:
firebase.database()
.ref(`/users/${currentUser.uid}/products/${productKey}`)
.set({category: name});
where products have many other key-value pairs, and using set() overwrites the entire object, which is not what I want in this case.
I appreciate any help!
From the section Update specific fields:
To simultaneously write to specific children of a node without overwriting other child nodes, use the update() method.
In your snippet that would be:
firebase.database()
.ref(`/users/${currentUser.uid}/products/${productKey}`)
.update({category: name});
I have a Collection named "balance". I want to get the value of one document in the collection. In order to get only the latest element in the collection I use this query:
db.balance.find().sort({date: -1}).limit(1);
There's a column called 'value' and I want to get that.
db.balance.find().sort({date: -1}).limit(1).value; however does not show the data I want. It shows nothing:
What's wrong with it?
find returns a cursor. You'll need to convert it to an array in order to actually extract the value. Try this:
db.balance.find().sort({date: -1}).limit(1).toArray()[0].value;
This is, of course, much easier inside of meteor (either in code or via meteor shell) because you can do:
Balance.findOne({}, {sort: {date: -1}}).value;