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
Related
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.
We have an application that will store data on Firebase (database) that will then be queried later.
What is the correct format to store the data in.
The example data will be completedGames. They will have data such as:
UserId
TimeToComplete
GameData
Etc...
The query later will then look for all completed games by UserId. We want to ensure the data is collected in the best way possible to query later, rather than refactoring later.
In your case, first off - be sure you have a good reason to use Firebase over Firestore. Once you're confident you should stick with Firebase Realtime Database, look at the below excerpt of documentation. So, you might actually have 2 separate parent nodes, 1 for userId and another for games. Each game node's child is a particular game, which has a child tree of game users (by userId).
Flatten data
structures
If the data is instead split into separate paths, also called
denormalization, it can be efficiently downloaded in separate calls,
as it is needed. Consider this flattened structure:
{
// Chats contains only meta info about each conversation
// stored under the chats's unique ID
"chats": {
"one": {
"title": "Historical Tech Pioneers",
"lastMessage": "ghopper: Relay malfunction found. Cause: moth.",
"timestamp": 1459361875666
},
"two": { ... },
"three": { ... }
},
// Conversation members are easily accessible
// and stored by chat conversation ID
"members": {
// we'll talk about indices like this below
"one": {
"ghopper": true,
"alovelace": true,
"eclarke": true
},
"two": { ... },
"three": { ... }
},
// Messages are separate from data we may want to iterate quickly
// but still easily paginated and queried, and organized by chat
// conversation ID
"messages": {
"one": {
"m1": {
"name": "eclarke",
"message": "The relay seems to be malfunctioning.",
"timestamp": 1459361875337
},
"m2": { ... },
"m3": { ... }
},
"two": { ... },
"three": { ... }
}
}
We have created a Custom Activity with Marketo web portal, and we are able to create leads with REST API. So now, how can we log Custom Activity data using Marketo REST API?
Custom Activity structure is as follows:
{
"id": 100001,
"name": "TEST_ACTIVITY",
"apiName": "test_api_c",
"description": "",
"primaryAttribute": {
"name": "event_id",
"apiName": "event_id",
"dataType": "string"
},
"attributes": [
{
"name": "email",
"apiName": "email",
"dataType": "email"
},
{
"name": "event_data",
"apiName": "event_data",
"dataType": "string"
}
]
}
You can push your custom activity records to the Add Custom Activities endpoint, which is available at the POST /rest/v1/activities/external.json url.
First, worth to note, that in order to use the endpoint, the API user has to have the “Read-Write Activity” permission.
The endpoint expects a payload with a single input key, that can hold up to 300 activity records. For each activity record the leadId, activityDate, activityTypeId, primaryAttributeValue and attributes fields are required, and in case of the attributes array, the name is the normal “name” field, not the “apiName”.
In your case the payload would look something like this:
{
"input":[// Note the array of records
{
"activityDate":"2018-03-20T22:43:12+02:00",
"activityTypeId":100001,
"leadId":<LEAD_ID>,
"primaryAttributeValue":"<YOUR_EVENT_ID>",
"attributes":[
{
"name":"email",// This is the `name` field of the attribute, not the `apiName`!
"value":"<EMAIL_ADDRESS>"
},
{
"name":"event_data",// This is the `name` field of the attribute, not the `apiName`!
"value":"<EVENT_DATA>"
}
]
},
//{
// …other activity records…
//}
]
}
I'm stuck with querying Firestore REST API. I can figure out how to filter by a regular key but I can't figure out how to query by nested object key.
Basically, in the browser client, I can do
firestore.collection("channels")
.where("members.[memberID]", "==", true)
.get()
However, when I try to do the same in the REST API, it said Invalid Argument. I was trying to do
{
"structuredQuery": {
"where": {
"fieldFilter": {
"field": {
"fieldPath": "members.[memberID]"
},
"op": "EQUAL",
"value": {
booleanValue: "true"
}
}
},
"from": [
{
"collectionId": "channels"
}
]
}
}
but it gives me "Invalid Argument" error on fieldPath. Does anybody know how to query Firestore REST API based on nested object?
Thank you!
I asked for Firebase support and they said special characters in fieldPath such as "-" in my case can be escaped using back tick or back slash. It is mentioned in the documentation. In my case, I'll need to do
members.`[memberID]`
I hope this is helpful for you guys.
Is it possible to limit the depth of data returned from Firebase database?
For example, if I want to get some data from a parent object without waiting for all of its children & sub-children, can I specify that I only want x levels of objects?
Firebase does not yet have this capability.
We do intend to add it, but don't have a timetable yet.
There appears to be a shallow option...
{
"message": {
"user": {
"name": "Chris"
},
"body": "Hello!"
}
}
// A request to /message.json?shallow=true
// would return the following:
{
"user": true,
"body": true
}
// A request to /message/body.json?shallow=true
// would simply return:
"Hello!"
From: https://firebase.google.com/docs/database/rest/retrieve-data