Nodejs per client mongoose schema - json

So I want to offer my users the ability to upload CSV and from that generate a mongoose schema, that I store in the DB against that user. When the user logs in, they can create a collection according to their personal schema. Using generate-schema I am able to create a json schema which looks like:
{
"_id" : ObjectId("596a872cd1e59c6135fa7b2e"),
"title" : "Product Set",
"type" : "array",
"items" : {
"type" : "object",
"properties" : {
"booktitle" : {
"type" : "string"
},
"bookid" : {
"type" : "string"
},
"bookauthor" : {
"type" : "string"
}
},
"required" : [
"booktitle",
"bookid",
"bookauthor"
],
"title" : "Product"
},
"$schema" : "http://json-schema.org/draft-04/schema#"
}
and store that in my schema collection. All good...
When I want to create a collection according to that schema, and store data in it using mongoose, I have tried to retrieve the schema from the database (which works) and then do
var generatedSchema = GenerateSchema.mongoose(response)
I then create a model from that with:
var Model = db.models.Product || db.model('Product', generatedSchema);
and create an item from that model
var item = new Model({
"_id": new ObjectID(),
booktitle: 'The Godfather',
bookid: 'abc123',
bookauthor: 'Mario Puzo'
});
and save it:
item.save(function(err, response) { ... })
I don't get any errors but when I save it, in the collection I just see:
{
"_id" : ObjectId("5970b1a584d396d7a2241eba"),
"items" : {
"required" : []
},
"__v" : 0
}
Can anyone point me in the right direction as to why this isn't working? My suspicion is I am using the wrong type of schema to create the model.
If someone has an answer to the above, how would you then go about creating methods on the schema, as you would if the schema was static and part of the codebase of the application?
Thanks

Related

NiFi: Extract Content of FlowFile and Add that Content to the Attributes

I am generating random data from the following JSON/AVRO schema:
{
"type" : "record",
"namespace" : "test",
"name" : "metro_data",
"fields": [
{
"name" : "PersonID",
"type" : "int"
},
{
"name" : "TripStartStation",
"type" : {
"type" : "enum",
"name" : "StartStation",
"symbols" : ["WIEHLE_RESTON_EAST", "SPRING_HILL", "GREENSBORO"]
}
},
{
"name" : "TripEndStation",
"type" : {
"type" : "enum",
"name" : "EndStation",
"symbols" : ["WIEHLE_RESTON_EAST", "SPRING_HILL", "GREENSBORO""]
}
}
]
}
The above schema generates this, for example:
[ {
"PersonID" : -1089196095,
"TripStartStation" : "WIEHLE_RESTON_EAST",
"TripEndStation" : "SPRING_HILL"
}
I want to take the PersonID number of the schema, and add it to the Attributes. Eg, the blank in this photo needs to pull the actual PersonID number generated from the flow:
I have tried to use EvaluateJSONPath with the following configuration, and that's how I end up with the empty string set under PersonalID:
Is my next processor UpdateAttribute? Not sure how to pull that content. Thanks!
You are having array of json message(s)(ex: [...]) and You need to split the array of json into individual flowfiles using SplitJson processor with split expression as $.*
Then use EvaluateJsonProcessor to extract PersonID value as a attribute.
Flow:
--> SplitJson --> EvaluateJsonPath--> other processors
For more details refer to this link regards to the same issue.

How to compare API request JSON with existing json document in MongoDb?

The title of the question is self explanatory. I want to know what differences are there in JSON Document A which comes from API request and JSON Document B which is already in Mongo DB.how to get changes column name and data also.. i am creating log..that's why i want...
Below is the code of what I'm trying:
NodeJS APICode//
function Updatejob(req, res) {
return function (jobSchedule) {
var obj = new Date();
CompareJSON(req, mongodbjson);
return Job.create(req.body).then(.....)
}
Already Data in Mongodb before Update Record
{
"_id" : ObjectId("586d1032aef194155028e9c7"),
"history" : [
{
"_id" : ObjectId("586d1032aef194155028e9c4"),
"updated_by" : "",
"details" : "Job Created",
"changetype" : "Created",
"datetime" : ISODate("2017-01-04T15:09:38.465Z")
}
],
"current_status" : "Pending",
"time" : 0
}
//REQUEST FOR UPDATE DATA
{
"_id" : ObjectId("586d1032aef194155028e9c7"),
"history" : [
{
"_id" : ObjectId("586d1032aef194155028e9c4"),
"updated_by" : "",
"details" : "Job Completed",
"changetype" : "Completed",
"datetime" : ISODate("2017-01-04T15:09:38.465Z")
}
],
"current_status" : "Completed",
"time" : 0
}
You can use jsondiffpatch:
var delta = jsondiffpatch.diff(object1, object2);
See:
https://www.npmjs.com/package/jsondiffpatch

JSON schema for an object whose value is an array of objects

I am writing a software that can read the JSON data from a file. The file contains "person" - an object whose value is an array of objects. I am planning to use a JSON schema validating libraries to validate the contents instead of writing the code myself. What is the correct schema that conforms to JSON Schema Draf-4 which represents the below data?
{
"person" : [
{
"name" : "aaa",
"age" : 10
},
{
"name" : "ddd",
"age" : 11
},
{
"name" : "ccc",
"age" : 12
}
]
}
The schema that wrote down is given below. I am not sure whether it is correct or is there any other form?
{
"person" : {
"type" : "object",
"properties" : {
"type" : "array",
"items" : {
"type" : "object",
"properties" : {
"name" : {"type" : "string"},
"age" : {"type" : "integer"}
}
}
}
}
}
You actually only have one line in the wrong place, but that one line breaks the whole schema. "person" is a property of the object and thus must be under the properties keyword. By putting "person" at the top, JSON Schema interprets it as a keyword instead of a property name. Since there is no person keyword, JSON Schema ignores it and everything below it. Therefore, it is the same as validating against the empty schema {} which places no restrictions on what a JSON document can contain. Any valid JSON is valid against the empty schema.
{
"type" : "object",
"properties" : {
"person" : {
"type" : "array",
"items": {
"type" : "object",
"properties" : {
"name" : {"type" : "string"}
"age" : {"type" : "integer"}
}
}
}
}
}
By the way, there are several online JSON Schema testing tools out there that can help you out when crafting your schemas. This one is my goto http://jsonschemalint.com/draft4/#
Also, here is a great JSON Schema reference that might help you out as well: https://spacetelescope.github.io/understanding-json-schema/

mongo db remove json objects

I have a mongo json object as follows
{
"_id" : new BinData(3, "RDHABb22XESWvP83FplqJw=="),
"name" : "NEW NODE",
"host" : null,
"aet" : null,
"studies" : ["1.3.12.2.1107.5.99.3.30000008061114424970500000589"],
"testcases" : [new BinData(3, "Zhl+zIXomkqAd8NIkRiTjQ==")],
"sendentries" : [{
"_id" : "1.3.12.2.1107.5.99.3.30000008061114424970500000589",
"Index" : 0,
"Type" : "Study"
}, {
"_id" : "cc7e1966-e885-4a9a-8077-c3489118938d",
"Index" : 1,
"Type" : "TestCase"
}]
}
The fields "Studies" and "TestCases" are now obsolete and I am now storing that information in a new field called SendEntries. I would like to get rid of the Studies and TestCases from the old entries and unmap those fields going forward. I want to know how I can update my current collections to get rid of the Studies and TestCases fields.
I'm just few weeks into Mongo.
You can use the $unset operator with update.
db.collection.update({},
{ $unset: {
"studies": "",
"testcases": ""
},
{ "upsert": false, "muti": true }
)
And that will remove all of the fields from all of your documents in your collection
Use $unset, there's a manual page e.g.
db.yourCollection.update( { },
{ $unset: {
Studies: "",
testcases: ""
}
},
{ multi: true }
)

Sencha Touch nested JSON - Store load with Associations

I want to load nested JSON in a Store, but the data is not correctly mapped. No problem with a single regModel but I can´t get the associations to work.
// JSON in test.json
{"message" : {
"returnCodes": [
{
"value": "0",
"code": "200",
"description": "OK"
},
{
"value": "0",
"code": "200",
"description": "OK"
}
]
}}
// Model with associations
Ext.regModel("ReturnCode", {
fields : [{
name : "value",
type : "string"
}, {
name : "code",
type : "string"
}, {
name : "description",
type : "string"
}],
belongsTo: "Message"
});
Ext.regModel("Message", {
hasMany: {
model : "ReturnCode",
name : "returnCodes"
}
});
// Store
var jobStore = new Ext.data.Store({
model : 'Message',
autoLoad: true,
proxy : {
type : 'ajax',
url: 'test.json',
reader : {
type : 'json',
root : 'message.returnCodes'
}
}});
// List
var list = Ext.extend( Ext.List, {
fullscreen : true,
store : jobStore,
grouped : false,
itemTpl : '<div>{code}</div>' // no output
});
When I look into the store every data is stored in the raw section of the store object but nothing in the data section. In the list for both returnCode Objects a listitem is created but they are not filled with data, because the mapping didn't succeed -> itemTpl gets no data.
Try declaring Message Model first, then add associationKey:'returnCodes' in hasMany{} within the Message Model. Also change the root of the reader to message.
This reference could also be of use to you.