Reputation: 31
I want to add a decimal number as key of a document in Firestore. I tried to do it like this:
key = 2.22
key = key + 0.01
db.collection('collection').doc('doc').update({ [key] : value })
but this way I get the key as {2 : { 23 : value } }
.
I wanted to get it like { 2.23 : value }
.
How can I achieve this?
Upvotes: 2
Views: 661
Reputation: 598740
The update()
method interprets the .
as addressing a nested field. So a.b.c
would be a field c
inside a field b
inside field a
.
There is no way (that I know of) to prevent Firestore from interpreting the .
in calls to update()
. Luckily calls to set()
are not interpreted in the same way, and you can use set
combined with merge: true
to get the same update behavior without it interpreting dots.
db.collection('collection').doc('doc').set({ [key] : value }, { merge: true })
Upvotes: 7
Reputation: 686
The problem of using .
in a field ID is that it require escaping like said in the firestore documentation
So if you really want the dot you should escape it like so \.
because if you don't firestore understand A.B
like the item of key B
in the map of key A
.
Another solution if you can limit the decimals of your float key is to transform your float into an integer so you do not have the problem of the dot notation.
For example if you can have only 2 decimals: 2.22
will be 222
(2.22 * 100
).
Upvotes: 0