Sencha Touch nested JSON - Store load with Associations - json

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.

Related

Loading nested JSON from Instana Eum into JQUERY datatables

I have read plenty of resources and questions here regarding nested JSON but none are asking the exact same.
I am trying to use IBM Instana to retrieve the page load metrics what I plan to load into a JQUERY DataTables table.
The nested JSON I get from Instana:
{
"items" : [ {
"name" : "/Content/Search.htm",
"earliestTimestamp" : 1674432701496,
"cursor" : {
"#class" : ".IngestionOffsetCursor",
"ingestionTime" : 1674509519588,
"offset" : 1
},
"metrics" : {
"pageLoads.sum" : [ [ 1674511200000, 79.0 ] ]
}
}, {
"name" : "/Content/Home.htm",
"earliestTimestamp" : 1674435256403,
"cursor" : {
"#class" : ".IngestionOffsetCursor",
"ingestionTime" : 1674509519588,
"offset" : 2
},
"metrics" : {
"pageLoads.sum" : [ [ 1674511200000, 45.0 ] ]
}
} ],
"canLoadMore" : false,
"totalHits" : 2,
"totalRepresentedItemCount" : 2,
"totalRetainedItemCount" : 2,
"adjustedTimeframe" : {
"windowSize" : 169200000,
"to" : 1674511200000
}
}
and the code to load it and add it to a table in DataTables:
$(document).ready(function () {
$('#example').DataTable({
ajax: {
url: 'data/daily.txt',
dataSrc: 'items',
},
columns: [
{ data: 'cursor.offset' },
{ data: 'name' },
{ data: 'earliestTimestamp' },
{ data: 'metrics[0]' },
],
deferRender: true
});
});
While the whole thing is straightforward, even the nested cursor.offset with the dot, I fail to assign the two value in metrics\pageLoads.sum, especially that the nested pageLoads.sum also contains a dot and the values are in double square brackets without quotation marks. The two values (date and number of page loads) I would like to have in two separate columns, but as fail to load them at all, I went on to load them somehow into one cell, but even that does not work.
I have tried to add pageLoads.sum (then as 'metrics."pageLoads.sum"')in quotes, but didn't help, DataTables threw the error message "Requested unknown parameter 'metrics.....<and the variation I have tried>.
I have also tried:
{ data: 'metrics' }, this returns [object Object] in the html cells
{ data: 'metrics[0]' }, no error from DT, but cell remains empty
{ data: 'metrics[, ]' },no error from DT, but cell remains empty
Is there any way to access the two values within metrics\pageLoads.sum or do I have to change JSON structure before this can be done?
To split the contents of your nested arrays...
"pageLoads.sum" : [ [ 1674511200000, 45.0 ] ]
...into two columns, you can use the following:
columns: [
{ data: 'cursor.offset' },
{ data: 'name' },
{ data: 'earliestTimestamp' },
{
data: 'metrics.pageLoads\\.sum',
render: function (data, type, row) {
return data[0][0];
}
},
{
data: 'metrics.pageLoads\\.sum',
render: function (data, type, row) {
return data[0][1];
}
}
]
This uses the double-backslash I mentioned in a comment (ref.) and also two column renderers which handle the splitting of the nested arrays into 2 separate fields.
The end result looks like this:

Nodejs per client mongoose schema

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

Firebase how to make this kind of query in Swift

I have this JSON structure:
{
"groups" : {
"-KBxo9-RoY0eowWKeHkU" : {
"author" : "rsenov",
"members" : {
"-KBxo7ZU6McsmDOxyias" : true,
"-KBxo8_TUTW6NZze6xcd" : true,
"rsenov" : true
},
"name" : "Prueba 3"
}
},
"users" : {
"-KBxo7ZU6McsmDOxyias" : {
"avatar" : "owl2",
"groups" : {
"-KBxo9-RoY0eowWKeHkU" : true
},
"isUser" : false,
"name" : "Pepa"
},
"-KBxo8_TUTW6NZze6xcd" : {
"avatar" : "monkey",
"groups" : {
"-KBxo9-RoY0eowWKeHkU" : true
},
"isUser" : false,
"name" : "Lolas"
},
"rsenov" : {
"avatar" : "guest",
"groups" : {
"-KBxo9-RoY0eowWKeHkU" : true
},
"isUser" : true,
"name" : "Ruben",
}
}
}
and the security&rules file is:
{
"rules": {
".read": true,
".write": true,
"users": {
".indexOn": ["email", "groups"]
},
"groups": {
".indexOn": ["author", "name"]
}
}
}
I'm trying to run a query in order to get the ChildChanged snapshot:
DataService.dataService.USERS_REF.queryOrderedByChild("groups").queryEqualToValue(currentGroup.groupKey).observeEventType(.ChildChanged, withBlock: {snapshot in
print(snapshot)
})
DataService.dataService.USERS_REFcorresponds to the url that point to the "users" key, and currentGroup.groupKeyis equal to -KBxo9-RoY0eowWKeHkUin this case.
According to this query, I should get the snapshot of the child that has changed. For example, if I replace the user name "Pepa" to "Test", I should get the snapshot:
"-KBxo7ZU6McsmDOxyias" : {
"avatar" : "owl2",
"groups" : {
"-KBxo9-RoY0eowWKeHkU" : true
},
"isUser" : false,
"name" : "Test"
}
but this query never get's called...
Is there something wrong in my query?
"I'm trying to run a query in order to get the ChildChanged snapshot:" is a little odd.
You can query for data, or observe a node via ChildChanged.
If you just want to be notified of changes within the users node, add an observer to that node and when Pepa changes to Test, your app will be notified and provided a snapshot of the user node that changed.
var ref = Firebase(DataService.dataService.USERS_REF)
ref.observeEventType(.ChildChanged, withBlock: { snapshot in
println("the changed user is: \(snapshot.value)")
})
Oh, and no need for queryOrderedByChild since the snapshot will only contain the single node that changed.

Mongoose: Save a JSON with embedded documents, using schemas with references

everyone. I tried to save a JSON (RecordNameVersion) like this
{ "created":"2015-11-10 15:47:41.107Z",
"id_user" : "01",
"version" : "1",
"RecordName" : {"status" : "1", "name": "Paul"}
}
In my node application I have this for the schemas
var RecordNameVersion = new Schema({
_id : Number,
created : {type: Date, default: Date.now},
id_user : String,
version : { type: Number, min: 0 },
RecordName : {type: Schema.Types.ObjectId, ref: 'TaxonRecordName'}
});
var RecordName = new Schema({
trn_version : { type: Number, ref: 'RecordNameVersion' },
status : String,
name : String
});
When I try to post the Json, this error is generated.
"message": "RecordNameVersion validation failed",
"name": "ValidationError",
Is possible save all the JSON in one go? or is necessary put embedded the schema for Record Name?
I prefer different collections for RecordNameVersion y RecordName.
Or I should put the id for RecordNameVersion in the json for RecordName
Your error in saving RecordNameVersion is telling you the value of field
RecordName
wants to be a Reference, not some actual object. So synthesize its Reference as :
{
"_id": mongoose.Types.ObjectId("123456789000")
"created":"2015-11-10 15:47:41.107Z",
"id_user" : "01",
"version" : "1",
"RecordName" : mongoose.Types.ObjectId("123456789055")
}
then the corresponding RecordName save could be :
{
"_id": mongoose.Types.ObjectId("123456789055")
"status":"1",
"name" : "Paul",
"version" : "1",
"trn_version" : mongoose.Types.ObjectId("123456789000")
}
Notice the value of _id fields across the two documents

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 }
)