show json with jstree 3.0 - javascript

I've been trying to load a json file from apache, and show it's result with jstree, 3.0.0.
Here is my code:
$(function () {
$('#showTree').jstree({
'core' : {
'data' : {
'url' : function (node) {
return node= 'miley.json';
},
'data' : function (node) {
return { 'id' : node.id };
}
}
}
});
});
Know miley:
[
{ "id" : "ajson1", "parent" : "national", "text" : "Simple root node" },
{ "id" : "ajson2", "parent" : "le", "text" : "Root node 2" },
{ "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1" },
{ "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2" },
]
My console swears it receives the json, but i don't have a clue on finally showing the tree.
What am i missing? Any one help me? Thanks!!

Ivan Bozhanov, the jsTree plug in creator, answered me quickly at the jsTree google forums...
Here is my working code:
$('#showTree').jstree({
'core' : {
'data' : {
'url': 'miley.json',
'data' : function (node) {
return { 'id' : node.id };
}
}
}
});
Also, i made some changes to miley, and deleted the last comma in the first miley.json I posted above...:
[
{ "id" : "ajson1", "parent" : "#", "text" : "Simple root node" },
{ "id" : "ajson2", "parent" : "ajson1", "text" : "Root node 2" },
{ "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1" },
{ "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2" }
]

Related

How to update the values of a "Key-Value" pair in a nested array in MongoDB without adding a new element

I have the following data:-
**{
"_id" : ObjectId("5bf8048768d1e82d8bb4a477"),
"customer_code" : "c100003",
"vm_info" : [
{
"instanceId" : "i-0495d75742b839858",
"tags" : [
{
"Key" : "SIDs",
"Value" : "NWS"
},
{
"Key" : "OSTYPE",
"Value" : "WINDOWS"
},
{
"Key" : "BACKUP_SERVER_LOCATION",
"Value" : ""
},
{
"Key" : "OS_VERSION",
"Value" : "10"
}
]
},
{
"instanceId" : "i-017488cffca28cd70",
"tags" : [
{
"Key" : "OS_VERSION",
"Value" : "10"
},
{
"Key" : "BACKUP_SERVER_LOCATION",
"Value" : ""
},
{
"Key" : "OSTYPE",
"Value" : "WINDOWS"
},
{
"Key" : "SIDs",
"Value" : "NWS"
}
]
},
{
"instanceId" : "i-09d5db81657fe35d8",
"tags" : [
{
"Key" : "SIDs",
"Value" : "NWS"
},
{
"Key" : "BACKUP_SERVER_LOCATION",
"Value" : ""
},
{
"Key" : "OSTYPE",
"Value" : "WINDOWS"
},
{
"Key" : "OS_VERSION",
"Value" : "10"
}
]
}
]
}**
Now i want to update the value of "OS_VERSION" to "12" instead of "10". After updating it should look like this:-
**{
"_id" : ObjectId("5bf8048768d1e82d8bb4a477"),
"customer_code" : "c100003",
"vm_info" : [
{
"instanceId" : "i-0495d75742b839858",
"tags" : [
{
"Key" : "SIDs",
"Value" : "NWS"
},
{
"Key" : "OSTYPE",
"Value" : "WINDOWS"
},
{
"Key" : "BACKUP_SERVER_LOCATION",
"Value" : ""
},
{
"Key" : "OS_VERSION",
"Value" : "12"
}
]
},
{
"instanceId" : "i-017488cffca28cd70",
"tags" : [
{
"Key" : "OS_VERSION",
"Value" : "10"
},
{
"Key" : "BACKUP_SERVER_LOCATION",
"Value" : ""
},
{
"Key" : "OSTYPE",
"Value" : "WINDOWS"
},
{
"Key" : "SIDs",
"Value" : "NWS"
}
]
},
{
"instanceId" : "i-09d5db81657fe35d8",
"tags" : [
{
"Key" : "SIDs",
"Value" : "NWS"
},
{
"Key" : "BACKUP_SERVER_LOCATION",
"Value" : ""
},
{
"Key" : "OSTYPE",
"Value" : "WINDOWS"
},
{
"Key" : "OS_VERSION",
"Value" : "10"
}
]
}
]
}**
When i use Update operator with "$addToSet" a new Key-Value pair is added instead of updating the existing data like this:
**{
"_id" : ObjectId("5bf8048768d1e82d8bb4a477"),
"customer_code" : "c100003",
"vm_info" : [
{
"instanceId" : "i-0495d75742b839858",
"tags" : [
{
"Key" : "SIDs",
"Value" : "NWS"
},
{
"Key" : "OSTYPE",
"Value" : "WINDOWS"
},
{
"Key" : "BACKUP_SERVER_LOCATION",
"Value" : ""
},
{
"Key" : "OS_VERSION",
"Value" : "10"
},
{
"Key" : "OS_VERSION",
"Value" : "12"
}
]
}
]
}** I don't want it to be added as a new element ,instead it should just update the existing element.
How can i achieve the desired result. Can someone help on this.
first remove that key-value pair and and insert new one.

Changing order of object in object array with an element key in mongo collection object

I want to change order of objects in slides sub document with respect to the sort_order key in it.
For Eg: I want to move slide with title A3 above A2. ie. sort_order value of A3 (3) is changed to 2 and that of A2 will be changed to 3, and viceversa in case of moving down.
How to do ??
Below is the db object.
{
"_id" : ObjectId("5a3b8cc68884dd1140a9a5b8"),
"title" : "Sort Test",
"user_id" : ObjectId("59e08e45f081170f582e95cc"),
"status" : "active",
"slides" : [
{
"_id" : ObjectId("5a3b8cc68884dd1140a9a5b9"),
"sort_order" : 0,
"content" : "Sort Test",
"title" : "Sort Test"
},
{
"_id" : ObjectId("5a3b8cda8884dd1140a9a5ba"),
"sort_order" : 1,
"content" : "Text Contents here...",
"title" : "A1"
},
{
"_id" : ObjectId("5a3b8ce48884dd1140a9a5bb"),
"sort_order" : 2,
"content" : "Text Contents here...",
"title" : "A2"
},
{
"_id" : ObjectId("5a3b8cec8884dd1140a9a5bc"),
"sort_order" : 3,
"content" : "Text Contents here...",
"title" : "A3"
},
{
"_id" : ObjectId("5a3b8cec8884dd1140455bc"),
"sort_order" : 4,
"content" : "Text Contents here...",
"title" : "A4"
}
],
"description" : "Sort Test",
"__v" : 0
}
With this method you can create a new array with same elements and the ones you ask for, switched (I think is what you asked).
Then you just have to update slides field in the DB with new array.
var dbObject = {
"_id" : ObjectId("5a3b8cc68884dd1140a9a5b8"),
"title" : "Sort Test",
"user_id" : ObjectId("59e08e45f081170f582e95cc"),
"status" : "active",
"slides" : [
{
"_id" : ObjectId("5a3b8cc68884dd1140a9a5b9"),
"sort_order" : 0,
"content" : "Sort Test",
"title" : "Sort Test"
},
{
"_id" : ObjectId("5a3b8cda8884dd1140a9a5ba"),
"sort_order" : 1,
"content" : "Text Contents here...",
"title" : "A1"
},
{
"_id" : ObjectId("5a3b8ce48884dd1140a9a5bb"),
"sort_order" : 2,
"content" : "Text Contents here...",
"title" : "A2"
},
{
"_id" : ObjectId("5a3b8cec8884dd1140a9a5bc"),
"sort_order" : 3,
"content" : "Text Contents here...",
"title" : "A3"
},
{
"_id" : ObjectId("5a3b8cec8884dd1140455bc"),
"sort_order" : 4,
"content" : "Text Contents here...",
"title" : "A4"
}
],
"description" : "Sort Test",
"__v" : 0
}
var slides = dbObject.slides;
var switchElementsInArray = function(array, sortOrder1, sortOrder2) {
var newArray = [];
for(var element of array) {
if(element.sort_order === sortOrder1) {
newArray.push(elem2);
} else if(element.sort_order === sortOrder2) {
newArray.push(elem1);
} else {
newArray.push(element);
}
}
return newArray;
}
var newSlides = switchElementsInArray(slides, 2, 3);

cannot use the part (stats of stats.cat3.detail.name) to traverse the element. nested objects and arrays

I'm able to do
db.getCollection('comps').find({"stats.cat3.detail.name" : "Friendlyness"})`
This shows the proper docs the have a name of "Friendlyness" in the detail array.
I wanted to change "Friendlyness" to "friendliness"
I tried :
db.getCollection('comps').update(
{"stats.cat3.detail.name" : "Friendlyness"},
{$set : {"stats.cat3.detail.name" : "Friendliness"} }
)
and got errors like
" cannot use the part (stats of stats.cat3.detail.name) to traverse the element ({stats: [ { cat3: ...."
I tried putting in position operators "$" all over the update object property. but still get errors.
sample data:
{
"_id" : ObjectId("58d94c441eb9e52454932db6"),
"desc" : "Manufacturer of a wide range of consumer electronics products including audio, video, communications, and computer systems.",
"source" : "alexa",
"category" : "Consumer Electronics",
"name" : "comp1.com",
"__v" : 0,
"stats" : [
{
"cat1" : {
"detail" : [
{
"value" : [
{
"data" : 1,
"name" : "quick service"
}
],
"name" : "Time on hold"
},
{
"value" : [
{
"data" : 1,
"name" : "some words but strong accent"
},
{
"data" : 1,
"name" : "EZ to understand"
}
],
"name" : "Language fluency"
},
{
"value" : [
{
"data" : 1,
"name" : "Yes"
}
],
"name" : "Human answered"
},
{
"value" : [
{
"data" : 2,
"name" : "Like a hot potato"
},
{
"data" : 1,
"name" : "1 or 2 times"
}
],
"name" : "Transfered alot"
},
{
"value" : [
{
"data" : 3,
"name" : "ok time"
},
{
"data" : 1,
"name" : "quick service"
},
{
"data" : 1,
"name" : "short time"
}
],
"name" : "Time on Hold"
},
{
"value" : [
{
"data" : 1,
"name" : "some word but strong accent"
},
{
"data" : 2,
"name" : "EZ to understand"
}
],
"name" : "Language Fluency"
},
{
"value" : [
{
"data" : 2,
"name" : "Yes"
}
],
"name" : "Human Answered"
}
],
"average" : 81
},
"cat2" : {
"detail" : [
{
"value" : [
{
"data" : 1,
"name" : "It was ok"
},
{
"data" : 1,
"name" : "Not EZ at all"
},
{
"data" : 7,
"name" : "there were hidden gimicks"
},
{
"data" : 2,
"name" : "It was OK"
},
{
"data" : 1,
"name" : "Found Quickly"
}
],
"name" : "Easy to find desired product"
},
{
"value" : [
{
"data" : 1,
"name" : "Easy to understand"
},
{
"data" : 1,
"name" : "Eventually understood"
}
],
"name" : "Clear information about the product"
},
{
"value" : [
{
"data" : 2,
"name" : "none"
},
{
"data" : 3,
"name" : "There was but won't complain"
},
{
"data" : 1,
"name" : "Very Bad"
}
],
"name" : "Annoying popups"
},
{
"value" : [
{
"data" : 4,
"name" : "Eventually Understood"
},
{
"data" : 1,
"name" : "EZ to understand"
}
],
"name" : "Clear data about the product"
}
],
"average" : 71
},
"cat3" : {
"detail" : [
{
"value" : [
{
"data" : 1,
"name" : "Very quick"
}
],
"name" : "Prompteness"
},
{
"value" : [
{
"data" : 3,
"name" : "We're cool"
},
{
"data" : 4,
"name" : "They didn't like me"
}
],
"name" : "Friendlyness"
},
{
"value" : [
{
"data" : 3,
"name" : "Very Confusing"
},
{
"data" : 2,
"name" : "Still a little confused"
},
{
"data" : 3,
"name" : "Enlightened"
}
],
"name" : "Knowledge"
},
{
"value" : [
{
"data" : 3,
"name" : "On hold too many times"
}
],
"name" : "Promptness"
}
],
"average" : 69
}
}
]
}

Convert nested document collection to model tree structure with parent references

I need to convert a collection with nested documents to a model tree structure with parent references. So this is how my structure looks like:
{
"_id" : "sdaGREsfRdfGdFdwG",
"docTitle" : "Document 1",
"group" : [
{
"id" : "cdPhkTpMXi8z6TqMT"
"title" : "title 1",
"data" : [
{
"id" : "Nkspf5kKfPo3axeJA",
"some" : "data",
"other" : "things",
"and" : "so on",
},
{
"id" : "Ca8ncFgq83RoeD8he",
"some" : "data",
"other" : "things",
"and" : "so on",
},
{
"id" : "vyjgkuNXRN9KkCd5o",
"some" : "data",
"other" : "things",
"and" : "so on",
}
],
},
{
"id" : "TuibXPe5qMvqdMW6q"
"title" : "title 2",
"data" : [
{
"id" : "f5L5zsSNRSQKWoAXL",
"some" : "data",
"other" : "things",
"and" : "so on",
}
],
}
]
}
As you can see, there is a group array, which has also a data array. Now I need a main document like this:
{
"_id" : "sdaGREsfRdfGdFdwG",
"docTitle" : "Document 1"
}
And the next child elements, which would be:
{
"id" : "cdPhkTpMXi8z6TqMT"
"title" : "title 1",
"type" : "group",
"parent" : "sdaGREsfRdfGdFdwG"
},
{
"id" : "TuibXPe5qMvqdMW6q"
"title" : "title 2",
"type" : "group",
"parent" : "sdaGREsfRdfGdFdwG"
}
At least the data elements, which are children of the group-elements:
{
"id" : "Nkspf5kKfPo3axeJA",
"some" : "data",
"other" : "things",
"and" : "so on",
"type" : "element",
"parent" : "cdPhkTpMXi8z6TqMT"
},
{
"id" : "Ca8ncFgq83RoeD8he",
"some" : "data",
"other" : "things",
"and" : "so on",
"type" : "element",
"parent" : "cdPhkTpMXi8z6TqMT"
},
{
"id" : "vyjgkuNXRN9KkCd5o",
"some" : "data",
"other" : "things",
"and" : "so on",
"type" : "element",
"parent" : "cdPhkTpMXi8z6TqMT"
},
{
"id" : "f5L5zsSNRSQKWoAXL",
"some" : "data",
"other" : "things",
"and" : "so on",
"type" : "element",
"parent" : "TuibXPe5qMvqdMW6q"
}
So my attempt would be something like this:
Collection.find({}).forEach(function(doc) { // get all documents
var docID = doc._id;
doc.group.forEach(function(group) { // get all groups
var parent = group.id;
group.type = "group";
group.parent = docID;
group.data.forEach(function(element) { // get all elements
element.type ="element";
element.parent = parent;
});
});
});
1) I don't know how to do that in a better way
2) And I don't know how to save the result back to the collection
Fot the groups:
db.myCol.aggregate([{$unwind:"$group"},
{$project:{_id:"$group.id", some:"$group.some",
parent:"$_id", type:{$literal:"group"}}}])
For the Elements:
db.myCol.aggregate([{$unwind:"$group"},
{$unwind:"$group.data"},
{$project:{_id:"$group.data.id", some:"$group.data.some",
parent:"$group.id", type:{$literal:"element"}}}])
And to your second question: You can add an $out step to the aggregation pipeline at the end. For example: {$out:"outputCol"}

unable to "skip" mongoose sub-documents

I am using mongoose and node and I am trying to paginate data from sub-documents. I am able to limit subdocuments, but not skip them.
The versions I am using are:
Mongo 3.0.0
Node 0.10.33
Mongoose 3.9.7
The Data
[{
"name" : "Ranger Table",
"_id" : ObjectId("550234d3d06039d507d238d8"),
"body" : [
{
"name" : "Jason",
"colour" : "Red",
"animal" : "T-rex",
"_id" : ObjectId("550234d3d06039d507d238de")
},
{
"name" : "Billy",
"colour" : "Blue",
"animal" : "Triceratops",
"_id" : ObjectId("550234d3d06039d507d238dd")
},
{
"name" : "Zach",
"colour" : "Black",
"animal" : "Mastadon",
"_id" : ObjectId("550234d3d06039d507d238dc")
},
{
"name" : "Tommy",
"colour" : "Green",
"animal" : "Dragon"
"_id" : ObjectId("550234d3d06039d507d238d9")
}
]
},{
"name" : "Bot Table",
"_id" : ObjectId("5502d205184cd74033f64e6b"),
"body" : [
{
"name" : "Optimus",
"team" : "Autobots",
"class" : "Leader",
"_id" : ObjectId("550234d3d06039d507d238d9")
},
{
"name" : "Bumblebee",
"team" : "Autobots",
"class" : "Scout",
"_id" : ObjectId("550234d3d06039d507d238da")
},
{
"name" : "Astrotrain",
"team" : "Decepticons",
"class" : "Transport",
"_id" : ObjectId("550234d3d06039d507d238db")
}
]
}]
The Code
var BodySchema = new Schema({random: String},{strict:false});
var FeedSchema = new Schema({
name: String,
body:[BodySchema]
});
var feed = mongoose.model('Feed', FeedSchema);
feed.find({_id:'550234d3d06039d507d238d8'})
.populate({
"path":"body",
"options":{
limit:2, //This works fine
skip:2 //This doesn't work
}
})
.exec(function(err, result){
if(err){return(res.send(500, err))}
res.send(result);
});
The Result
The above code DOES limit the number of "body" sub-documents to 2, but doesn't skip any.
The code above returns this:
{
"name" : "Ranger Table",
"_id" : ObjectId("550234d3d06039d507d238d8"),
"body" : [
{
"name" : "Jason",
"colour" : "Red",
"animal" : "T-rex",
"_id" : ObjectId("550234d3d06039d507d238de")
},
{
"name" : "Billy",
"colour" : "Blue",
"animal" : "Triceratops",
"_id" : ObjectId("550234d3d06039d507d238dd")
}
]
}
But it should return this:
{
"name" : "Ranger Table",
"_id" : ObjectId("550234d3d06039d507d238d8"),
"body" : [
{
"name" : "Zach",
"colour" : "Black",
"animal" : "Mastadon",
"_id" : ObjectId("550234d3d06039d507d238dc")
},
{
"name" : "Tommy",
"colour" : "Green",
"animal" : "Dragon"
"_id" : ObjectId("550234d3d06039d507d238d9")
}
]
}
The solution that I found was to use aggregate and specify the name of the subdocument with $unwind.
http://docs.mongodb.org/manual/reference/operator/aggregation/
feed.aggregate([
{'$match':{_id:id('550234d3d06039d507d238d8')}},
{'$unwind':'$body'},
{'$skip':2},
{'$limit':2},
], function(err, result){
if(err){return(res.send(500, err))}
res.send(result);
});

Categories