Create the structure of an empty collection in mongoDB - json

Is there any possibility to create the structure of an empty collection in MongoDB using mongoimport from a JSON file like the one below?
"Users" : {
"name" : "string",
"telephone" : {
"personal": { "type": "number" },
"job": { "type" : "number" }
},
"loc" : "array",
"friends" : "object"
}
My goal is to create a mongoDB schema from JSON files.

Yes, you can mongoimport a JSON file and if you clear out the values of those field (set them to ""), importing your JSON file should do just that.
However, MongoDB is a NoSQL database, and creating a schema in the MongoDB database doesn't really make sense. What will happen is that you'll have one record with fields whose values are empty.

Related

Storing JSON blob in an Avro Field

I have inherited project where an avro file that is being consumed by Snowflake. The schema of the avro is as follows:
{
"name": "TableName",
"namespace": "sqlserver",
"type": "record",
"fields": [
{
"name": "hAccount",
"type": "string"
},
{
"name": "hTableName",
"type": "string"
},
{
"name": "hRawJSON",
"type": "string"
}
]
}
The hRawJSON is a blob of JSON itself. The previous dev put this as a type of string, and this is where I believe the problem lies.
The application takes a JSON object (the JSON is varible so I never know the contents or what it contains) and populates the hRawJSON field in the Avro record. But it contains the escape characters for the double quotes in the string:
hAccount:"H11122"
hTableName:"Departments"
hRawJSON:"{\"DepartmentID\":1,\"ModelID\":0,\"Description\":\"P Medicines\",\"Margin\":\"3.300000000000000e+001\",\"UCSVATRateID\":0,\"References\":719,\"HeadOfficeID\":1,\"DividendID\":0}"
As a result the JSON blob is staged into Snowflake as a VARIANT field but still retains the escape characters:
Snowflake image
This means when querying the data in the JSON I constantly have to use this:
PARSE_JSON(RAW_FILE:hRawJSON):DepartmentID
I can't help feeling that the field type of string in the Avro file is causing the issue and that a different type should be used. I've tried Record, but without fields it's unuable. Doc also not working.
The other alternative is that this behavior is correct and when moving the hRawJSON from staging into "proper" tables I should use something like:
INSERT INTO DATA.PUBLIC.DEPARTMENTS
SELECT
RAW_FILE:hAccount::VARCHAR(4) as Account,
PARSE_JSON(RAW_FILE:hRawJSON) as JsonRaw
FROM DATA.STAGING.AVRO_RAW WHERE RAW_FILE:hTableName::STRING = 'Department';
So if this should be the correct approach and I'm over thinking this I'd appreciate guidance.

Importing CSV File in Elasticsearch

I am new to elasticsearch. Trying to import CSV file by following the guide
And successfully imported the file and it has created an index with the documents also.
But what I found that in every docs _id contains random unique id as a value. I want to have value of _id from the CSV file field (the CSV file which I'm importing contains a field with unique id for every row) using query or any other ways. And I do not know how to do that.
Even in docs it is not explained. A sample example of document of elasticsearch index is shown below
{
"_index" : "sample_index",
"_type" : "_doc",
"_id" : "nGHXgngBpB_Kjkqcxfj",
"_score" : 1.0,
"_source" : {
"categoryid" : "34128b58-9148-11eb-a8b3-0242ac130003",
"categoryname" : "Blogs",
"isdeleted" : "False"
}
while adding ingest pipeline with the following query
{
"set": {
"field": "_id",
"value": "{{categoryid}}"
}
}
it throwing an error with this message
You can achieve this by modifying the ingest pipeline used to ingest your CSV file.
In the Ingest pipeline area (Advanced section), simply add the following processor at the end of the pipeline and the document ID will be set accordingly:
...
{
"set": {
"field": "_id",
"value": "{{categoryid}}"
}
}
It should look like this:
Added following processor in ingest pipeline section and it works..
{
"processors": [
{
"set": {
"field": "_id",
"value": "{{categoryid}}"
}
}
]
}

Express JS fetch proper JSON with Mysql

I am working on WebAPI on ExpressJS. I am fetching data from MySQL database by using node module mysql. I have a mater and a detail table let say
Countries(ID, Name, Code)
States (ID, Name, CountryId)
Now I want to query the data in JSON as following:
{
"country":{
"ID" : 1,
"Name" : "United States of America"
"Code" : "USA"
"Cities" :
[
{
"1",
"Alabama",
"AL"
},
{
"2",
"Alaska",
"AK"
},
{
"3",
"California",
"CA"
}
]
}
}
How can I query data in the above format?
Thanks and Regards.
How can I query data in the above format?
You can't because this is not a valid JSON. No matter what you do, you will never be able to represent your data in that format.
To represent data coming from a relational database as a JavaScript nested objects and arrays, it will be easiest to use an ORM like Sequelize, Bookshelf, ORM2 or Waterline. See:
http://docs.sequelizejs.com/
http://bookshelfjs.org/
https://github.com/dresende/node-orm2
http://sailsjs.com/documentation/reference/waterline-orm
ORM is an Object-Relational Mapper and lets you map the relational model to objects, which is exactly what you're trying to do here.

JSON schema without "properties" keyword

Vendor send me a json schema. Please look at this:
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"type" : "object",
"definitions" : {
...
},
"oneOf" : [{
"$ref" : "#/definitions/commons/strings/text"
}, {
"$ref" : "#/definitions/dto/scriptStep"
}, {
"$ref" : "#/definitions/dto/callResult"
}
]
}
There is no "properties" keyword (but by the way there is very large "definitions" part). Does it mean that schema actually descibes empty json object {}? Or does it mean that json could contain one of elements from "oneOf" array?
All JSON Schema keywords are constraints. For example, the empty schema {} means that any JSON is valid. A schema with just `{ "type": "object" } mean that any JSON object is valid. There are no constraints on what properties the object has.
However, that isn't what your vendor is expressing in this schema. The JSON doesn't only have to be valid against the "type": "object", but also against one of the three schemas referenced in oneOf. Presumably, those schemas include a properties keyword.
This probably isn't the best designed schema, but it is valid.

Trying to make nested json and point to the same object using multiple keys

let say i have this JSON data
"value1" : { "name" : "Foo" }
"value2" : { "name" : "14" }
"value3" : { "gender" : "Male" }
now i am trying to do this
"value1', "value2", "value3" : { "name" : "Foo" }
or maybe this if at all possible
["value1', "value2", "value3"] : { "name" : "Foo" }
so in a nutshell i have data that i would like to access using multiple pointers point to the same data in a JSON formate so that i don't have to repeat the same data for different pointers
here is an example of data:
"Model 1" : { "E-Series" : ["Green", "Purple"] }
let say "Model 2" has same info as "Model 1" how can point "Model 2" to "Model 1" data object in JSON without repeating the same code over and over again
This is not possible in JSON format. You can simulate it in code for example for Model2 set "ref": "Model1" and then programmatically read data from object Model1.
JSON hasn't got this feature by design.
This is not correct JSON syntax, and JSON does not have provisions for links. Your options are for encoding object references are:
LD+JSON (http://json-ld.org/)
HAL+JSON (http://stateless.co/hal_specification.html)
JSON-R (http://java.dzone.com/articles/json-r-json-extension-deals)
dojox.json.ref (https://dojotoolkit.org/reference-guide/1.10/dojox/json/ref.html)
etc, etc, ...
your custom data model for references
The benefit of using something more or less standard is a better (future) integration, but it may not be relevant for your task.