Can I insert data in Firebase database with multiple children? - firebase

Can I insert data into my Firebase database with multiples children? Can I have like this JSON example where there are 2 children of the phones?
{
"person1": {
"id": 12345,
"name": "John Doe",
"phones": {
"home": "800-123-4567",
"mobile": "877-123-1234"
}
}
}

Yeah, why not? You can write to database like these (For Android):-
mDatabase.child("person1").child("phones").child("home").setValue("800-123-4567");
mDatabase.child("person1").child("phones").child("mobile").setValue("877-123-1234");

Related

How can document references be used for Firestore API's [runQuery.startAt]?

When using the Firestore REST API to order and filter results, I am unable to use a cursor/reference value for the startAt value. I feel this may be possible, seeing it is provided in examples of Firestore's cursor-based pagination is detailed in their SDK: https://firebase.google.com/docs/firestore/query-data/query-cursors
I have a query that uses orderBy on a integer field within a document. I can successfully start at a specific integer value for this query, like so:
"structuredQuery": {
"from": [
{
"collectionId": "objects"
}
],
"orderBy": [
{
"field": {
"fieldPath": "counter"
},
"direction": "DESCENDING"
}
],
"startAt": {
"values": [
{
"integerValue": 15
}
]
}
}
I'm hoping to benefit from cursor pagination, but unfortunately if I change the startAt value to referenceValue, the query results do not reflect this, even though the query is successfully performed.
"startAt": {
"values": [
{
"referenceValue": "projects/.../databases/(default)/documents/objects/OjvmBvvQ9TkyyQiJ4ExJ"
}
]
}
Am I missing something in the way this works differently to the SDK examples?
Note that it's not a document reference but a document snapshot that you can use as a query cursor. A snapshot includes the field values needed for startAt. The SDKs take a document snapshot, extract the field values, and pass those values to startAt.
You can see the Node.js client library doing this here in createCursor and extractFieldValues.

IN query in cloud firestore

Can I query multiple keywords in firestore? How I can match an array of keywords in firestore?
I have a collection of documents with a title, I want to query articles contains specific keywords.
Following is my document structure.
{
"users": {
"user_id_1": {
"username": "user one",
"profile_pic": "some_url",
"articles": {
"article_id_1": {
"title": "Firebase is so cool",
"comments": {
"comment_id_1": "First comment",
"comment_id_2": "I like trains"
}
},
"article_id_2": {
"title": "Firestore rocks!",
"comments": {
"comment_id_1": "SQL it's better",
"comment_id_2": "Do you know the wae?"
}
},
"article_id_3": {
"title": "Firestore awesome",
"comments": {
"comment_id_1": "SQL it's better",
"comment_id_2": "Do you know the wae?"
}
},
"article_id_4": {
"title": "Firestore is easy",
"comments": {
"comment_id_1": "SQL it's better",
"comment_id_2": "Do you know the wae?"
}
}
}
}
}
}
Here I want to search articles based on the following keywords.
["cool", "rocks", "Firestore is easy"]
I should get article_id_1, article_id_2 and article_id_4
Thanks.
This is not possible with Firestore alone. It's not a text search engine. You will want to export your data to a text search engine such as Algolia in order to perform text searches that are not based on simple text equality. The documentation suggests this solution.

Set server value now as timestamp

I'm trying to write the now timestamp using the rest api of firestore. The sending application doesn't keep the time so its up to the server to store the value.
{
"fields": {
"Type": {
"stringValue": "temperature"
},
"Timestamp": {
"timestampValue": "2019-02-18T23:00:00Z"
},
"Value": {
"integerValue": "17"
}
}
}
The values get stored properly but I want to be able to change the static 2019-02-18T23:00:00Z to the now value.
How can this be done?
To write the server-side timestamp in a field, you need to use a FieldTransform for that. See Firestore REST API add Timestamp and https://cloud.google.com/firestore/docs/reference/rest/v1/Write

Is this NoSql schema correct?

I start to use Firebase and NoSql, I'd like to understand best practice, so I try to create this schema with orders, order_rows and articles. The main query is to show specific order so I create this:
{
"orders": {
"1": {
"date": "1/1/2016",
"row_order": {
"1": true,
"2": true
}
},
"2": {
"date": "1/1/2016",
"row_order": {
"3": true
}
}
},
"articles": {
"1": {
"name": "a"
},
"2": {
"name": "b"
}
},
"row_orders": {
"1": {
"quantity": 7,
"id_article": 1
},
"2": {
"quantity": 2,
"id_article": 2
},
"3": {
"quantity": 4,
"id_article": 2
}
}
}
When I must show order number 1, I can find order rows and then from row orders detect articles.
I don't use classic sql schema (row_orders with id_article and id_order) to semplify get data.
Is this correct? Or How I can do?
There is no way to say of a NoSQL data structure is correct. If you want to learn about NoSQL, I recommend reading the Firebase documentation on structuring data, this article on NoSQL data modeling and some of the older blog posts on the Firebase blog (such as this one).
One immediate problem in your data structure is that you use pseudo array-indices as the keys for your data. These will lead to a hard time, since Firebase will try to interpret them as actual array indices (meaning you get back an array with an empty first element) and it doesn't scale over many users or users who may be offline. Read more about this in the blog post on arrays.

Firebase data schema guidance

I am newbie to Firebase and working on to create events using firebase. Here i am inviting my friends using their phone number.(It is not necessary that whom i am inviting will be part of the system user.)
Below is my schema:
{
"events": [
{
"message": "Lunch",
"startTime": 1469471400000,
"eventCreatorId": 1,
"endTime": 1469471400000,
"invitees": [
{
"phone": "1234567890",
"type": "phone"
},
{
"phone": "345678901",
"type": "phone"
}
]
}
]
}
Now problem is that how i can find list of all events for specific invites?? (i.e in above case i want to find list of all events for user with phone number eqaul to 345678901.)
Can anyone suggest good schema to handle above scenario with firebase?
Welcome to using a NoSQL database. :-)
In NoSQL you often end up modeling the data different, to allow the queries that you want your app to execute. In this case, you apparently want to both show the invitees per event and the events per invitee. If that is the case, you'll store the data in both formats:
{
"events": {
"event1": {
"message": "Lunch",
"startTime": 1469471400000,
"eventCreatorId": 1,
"endTime": 1469471400000,
"invitees": {
"phone_1234567890": true,
"phone_345678901": true
}
}
},
"users": {
"phone_1234567890": {
"phone": "1234567890",
"type": "phone",
"events": {
"event1": true
}
},
"phone_345678901": {
"phone": "345678901",
"type": "phone"
"events": {
"event1": true
}
}
}
}
You'll see that I've split your data into two separate top-level nodes: events and users. They refer to each other with so-called explicit indexes, essentially a set of foreign keys that you manage (and join) in your client-side code.
I've also replaced you arrays with named keys. If your events/users have natural keys (such as uid for identifying the user if you happen to use Firebase Authentication) you'd use that for the key. But otherwise, you can use Firebase push ids. Using such keys leads to a more scalable data structure then depending on array indices.
Both of these topics are covered in the Firebase documentation on data structuring. I also highly recommend this article on NoSQL data modeling.

Resources