Create Firestore database documents from Postman - json

I'm trying to create multiple documents at once in a Firestore collection with configuration purposes. I can create documents one by one sending from Postman:
{
"fields": {
"category":{"stringValue": "Configs"},
"order":{"integerValue":"3"}
}
}
I was wondering if there is some way to create multiple documents sending something like this:
{
"batch": [
{
"fields": {
"categoria": {
"stringValue": "Configurações"
},
"ordem": {
"integerValue": "3"
}
}
},
{
"fields": {
"categoria": {
"stringValue": "Configurações"
},
"ordem": {
"integerValue": "3"
}
}
}
]
}
Can anyone say if is it possible? If so, how can I do that?

Something like the below should work.
Although it is an "update" action is creates new documents as well if they don't exist. The document ID must be provided, it cannot be auto generated using the commit endpoint.
I was able to get my use case to work after viewing this response detailing use of commit.
API Endpoint: https://firestore.googleapis.com/v1/projects/projectID/databases/(default)/documents:commit
{
"writes": [
{
"update": {
"name": "projects/projectID/databases/(default)/documents/test/ExistingDocumentID1",
"fields": {
"comment": {
"stringValue": "Hello World!"
}
}
}
},
{
"update": {
"name": "projects/projectID/databases/(default)/documents/test/NewManualDocumentID2",
"fields": {
"comment": {
"stringValue": "Happy Birthday!"
}
}
}
}
]
}

Related

GraphQL Query returns null objects both in GraphiQL and App from JSON data source

I'm trying to get my mocked JSON data via GraphQL in Gatsby. The response shows the correct data, but also two null objects as well. Why is it happening?
I'm using the gatsby-transformer.json plugin to query my data and gatsby-source-filesystem to point the transformer to my json files.
categories.json
the mock file I'm trying to get to work :)
{
"categories": [
{
"title": "DEZERTY",
"path": "/dezerty",
"categoryItems": [
{
"categoryName": "CUKRIKY",
"image": "../../../../static/img/dessertcategories/cukriky.jpg"
},
{
"categoryName": "NAHODNE",
"image": "../../../../static/img/dessertcategories/nahodne.jpg"
},
]
},
{
"title": "CANDY BAR",
"path": "/candy-bar",
"categoryItems": [
{
"categoryName": "CHEESECAKY",
"image": "../../../../static/img/dessertcategories/cheesecaky.jpg"
},
{
"categoryName": "BEZLEPKOVÉ TORTY",
"image": "../../../../static/img/dessertcategories/bezlepkove-torty.jpg"
},
]
}
]
}
GraphQL query in GraphiQL
query Collections {
allMockJson {
edges {
node {
categories {
categoryItems {
categoryName
image
}
title
path
}
}
}
}
}
And the response GraphiQL gives me
{
"data": {
"allMockJson": {
"edges": [
{
"node": {
"categories": null
}
},
{
"node": {
"categories": null
}
},
{
"node": {
"categories": [
{
"categoryItems": [
{
"categoryName": "CHEESECAKY",
"image": "../../../../static/img/dessertcategories/cheesecaky.jpg"
},
{
"categoryName": "BEZLEPKOVÉ TORTY",
"image": "../../../../static/img/dessertcategories/bezlepkove-torty.jpg"
}
],
"title": "DEZERTY",
"path": "/dezerty"
},
{
"categoryItems": [
{
"categoryName": "CUKRIKY",
"image": "../../../../static/img/dessertcategories/CUKRIKY.jpg"
},
{
"categoryName": "NAHODNE",
"image": "../../../../static/img/dessertcategories/NAHODNE.jpg"
}
],
"title": "CANDY BAR",
"path": "/candy-bar"
}
]
}
}
]
}
}
}
I expected only to get the DEZERTY and CANDY BAR sections. Why are there null categories and how do I fix it?
Thanks in advance
Your JSON contains syntax errors in the objects DEZERTY and CANDY BAR. It silently fails without telling you. Try this json linter.
Error: Parse error on line 12: },
Error: Parse error on line 25: },
Try again. Your query should work now.
You should look into an IDE that highlights these types of errors and saves you time and frustration.

How to iterate through data from a JSON file in VueJS/Gridsome using GraphQL?

I am getting my feet wet in VueJS/Gridsome and I am trying to make a simple site to start off. What I am looking to do is store all my data locally in a JSON file and pull it into my page/component using GraphQL. I am able to see the data load however I cannot iterate through every object; instead it shows me all the data.
I have tried changing the way I layout my JSON objects schemas, tried separating into multiple json files (Do not want to do this) and have played with different v-for parameters. On the Gridsome Docs and Youtube Tutorials everyone is pulling from markdown and generating pages from each markdown file or using some sort of API. I want to accomplish this in a single JSON file.
videos.json
[
{
"title": "vid 1",
"url": "url1"
},
{
"title": "vid 2",
"url": "url2"
}
]
GraphQL Query
{
allVideo {
edges {
node{
id
data {
title
url
}
}
}
}
}
GraphQL Results
{
"data": {
"allVideo": {
"edges": [
{
"node": {
"id": "6b3cddf43cce8f8a0fb122d194db6edc",
"data": [
{
"title": "vid 1",
"url": "https://youtube.com"
},
{
"title": "vid 2",
"url": "https://youtube.com"
}
]
}
}
]
}
}
}
Videos.vue
<template>
<Layout>
<h1>Videos</h1>
<div v-for="edge in $page.allVideo.edges" :key="edge.node.id">
{{edge.node.data}}
</div>
</Layout>
</template>
<page-query>
query Videos {
allVideo {
edges {
node {
id
data {
title
url
}
}
}
}
}
</page-query>
Page Result:
Videos
[ { "title": "vid 1", "url": "url1" }, { "title": "vid 2", "url": "url2" } ]
I expect there to be an ID for each video entry, however it seems that the data collection is in the wrong scope and I cannot get it one level up to be with ID to generate an ID with each set of video.
I am looking for something that will give me this query result:
{
"data": {
"allVideo": {
"edges": [
{
"node": {
"id": "6b3cddf43cce8f8a0fb122d194db6edc",
"data": [
{
"title": "vid 1",
"url": "url1"
}
]
}
},
{
"node": {
"id": "some other ID",
"data": [
{
"title": "vid 2",
"url": "url 2"
}
]
}
}
]
}
}
}
You can do this by querying a single video using the GraphQL query below.
If you check the docs on GraphQL playground on http://localhost:8081/___explore you should see a query for Video and also allVideo. Instead of using allVideo in your vue components use a single video and pass in the ID you should get the result you want.
{
Video(id: "6b3cddf43cce8f8a0fb122d194db6edc") {
edges {
node{
id
data {
title
url
}
}
}
}
}

How to check in elasticsearch if a JSON object has a key using the DSL?

If I have two documents within an index of the following format, I just want to weed out the ones which have an empty JSON instead of my expected key.
A
{
"search": {
"gold": [1,2,3,4]
}
B
{
"search":{}
}
I should just get A json and not B json.
I've tried the exists query to search for "gold" but it just checks for non null values and returns the list.
Note: The following doesn't do what I want.
GET test/_search
{
"query": {
"bool": {
"must": [
{
"exists": { "field": "search.gold" }}
]
}
}
}
This is a simple question but I'm unable to find a way to do it even after searching through their docs.
If someone can help me do this it would be really great.
The simplified mapping of the index is :
"test": {
"mappings": {
"carts": {
"dynamic": "true",
"_all": {
"enabled": false
},
"properties": {
"line_items": {
"properties": {
"line_items_dyn_arr": {
"type": "nested",
"properties": {
"dynamic_key": {
"type": "keyword"
}
}
}
}
}
}
}
}
}
Are you storing complete json in search field?
If this is not the case then please share the mapping of your index and sample data.
Update: Query for nested field:
{
"query": {
"nested": {
"path": "search",
"query": {
"bool": {
"must": [
{
"exists": {
"field": "search.gold"
}
}
]
}
}
}
}
}
For nested type fields we need to specify the path and query to be executed on nested fields since nested fields are indexed as child documents.
Elastic documentation: Nested Query
UPDATE based on the mapping added in question asked:
{
"query": {
"nested": {
"path": "line_items.line_items_dyn_arr",
"query": {
"exists": {
"field": "line_items.line_items_dyn_arr"
}
}
}
}
}
Notice that we used "path": "line_items.line_items_dyn_arr". The reason we require to provide full path is because nested field line_items_dyn_arr is itself under line_items object. Had line_items_dyn_arr be a property of mapping and not the property of object or nested field the previous query would work fine.
Nishant's answer is right but for some reason I could get it working only if the path and field are the whole paths.
The following works for me.
{
"nested": {
"path": "search.gold",
"query": {
"exists": {
"field": "search.gold"
}
}
}
}

Why is the json code wrong?

Why is the json code wrong? I know I can have multi key in XML, but it seem that json doesn't allow.
{
"BackupSettings": {
"Setting":
{
"id": "34345"
},
"Setting": {
"id": "16454"
}
}
}
Indeed, keys within an object are required to be unique in JSON. The canonical way of expressing your data in JSON would be to use an array. It could look something like the following:
{
"BackupSettings": {
"Settings": [
{
"id": "34345"
},
{
"id": "16454"
}
]
}
}
Or even:
{
"BackupSettings": [
{
"id": "34345"
},
{
"id": "16454"
}
]
}

Query elasticsearch match return inexact document

I'm trying to retrieve random documents that contain #maga so I did the following query:
{
"_source": "text",
"query": {
"function_score": {
"query": {
"match": {
"text": "#maga"
}
},
"functions": [
{
"random_score": {}
}
]
}
}
}
The problem is some returned document doesn't contain #maga but just the token maga. Why so? And How can I ivercome this problem?