I have 2 collections and I am new to Mongo MapReduce concept.
My collection Value looks like this
{
"_id" : ObjectId("5bc4f20e1a80d2045029ccb7"),
"name" : "Title 1",
"value" : "value 1 for title 1"
}
{
"_id" : ObjectId("5bc4f20e1a80d2045029ccb8"),
"name" : "Title 1",
"value" : "Value 2 for title1. "
}
{
"_id" : ObjectId("5bc4f20e1a80d2045029ccb9"),
"name" : "Title 2",
"value" : " Definitely a motivational text!"
}
{
"_id" : ObjectId("5bc4f20e1a80d2045029ccba"),
"name" : "Title 2",
"value" : "It's tough but I'm carrying on. "
}
Other collection Details look like:
{
"_id" : ObjectId("5bc4f2361a80d2045029d05b"),
"name" : "Title 1",
"totalValues" : 6.0000000000000000,
"link": "Link1",
"description" : "Some More Text to Make title 1 look better"
}
{
"_id" : ObjectId("5bc4f2361a80d2045029d2eb"),
"name" : "Title 2",
"totalValues" : 2,
"link": "Link2",
"description" : "Some More Text to Make title 2 look better"
} ...
I want the output result as
{
“name” : “XXXXXXXX”,
“comments” : [ {“value”: “XXXXXXX”}, {“value”: “YYYYYYYY”}],
“totalValues” : 2,
“link”: “XXXXXXXXXX”,
“description”: “XXXXXXXXXXXXXXXXXX”
}
with the following output document. Please help.
I have tried following code:
var mapDetails = function(){
var output = {name: this.name,totalValues :this.totalValues, description : this.description, link :this.link}
emit(this.name, output);
};
var mapGpas = function() {
emit(this.name,{name:this.name, value:this.value, totalValues:0, description: 0, link: 0});
};
var r = function(key, values) {
var outs = { name: 1,value: 1, description: 0,totalValues:0, link: 0};
values.forEach(function(v){
outs.name = v.name;
if (v.value == 0) {
outs.description = v.description;
outs.totalValues = v.totalValues;
outs.link = v.link;
}
});
return outs;
};
res = db.Value.mapReduce(mapGpas, r, {out: {reduce: 'joined'}})
res = db.Details.mapReduce(mapDetails, r, {out: {reduce: 'joined'}})
db.joined.find().pretty()
Related
I have a schema for Forms and another one for Documents...
Each Document must have a reference _id of the Form that is related to,
so in database I can have many Documents with the same Form._id.
I want to know how to create a DELETE function for the Forms to do the following:
Find all Documents that have this Form._id and DELETE it all and then
delete the Form itself.
Ok, so lets start of with some basic forms and documents in our database
db.forms.insertMany([
{ _id : 1, name : "Form 1"},
{ _id : 2, name : "Form 2"}
]);
db.documents.insertMany([
{ _id : 1, formId : 1, name : "Doc 1" },
{ _id : 2, formId : 1, name : "Doc 2" },
{ _id : 3, formId : 1, name : "Doc 3" },
{ _id : 4, formId : 2, name : "Doc 4" }
])
> db.forms.find()
{ "_id" : 1, "name" : "Form 1" }
{ "_id" : 2, "name" : "Form 2" }
> db.documents.find()
{ "_id" : 1, "formId" : 1, "name" : "Doc 1" }
{ "_id" : 2, "formId" : 1, "name" : "Doc 2" }
{ "_id" : 3, "formId" : 1, "name" : "Doc 3" }
{ "_id" : 4, "formId" : 2, "name" : "Doc 4" }
Then we can create a simple function to pass in the form id and find and delete all the documents for that form id.
function removeForm(formId){
var ids = db.documents.find({ formId : formId}, { _id : 1 } ).map(function(doc){ return doc["_id"]; });
db.documents.remove( { "_id" : { "$in" : ids } } );
db.forms.remove( { "_id" : formId } );
}
Then we can call removeForm.
removeForm(1);
Then we'll see our documents and forms deleted
> db.forms.find()
{ "_id" : 2, "name" : "Form 2" }
> db.documents.find()
{ "_id" : 4, "formId" : 2, "name" : "Doc 4" }
In our application we do it like that:
When a user got deleted, delete also its corrosponding Scans & Categories
const scanModel = require("./Scan");
const categoryModel = require("./Category");
const userSchema = new mongoose.Schema({
...
})
userSchema.pre("remove", function(next) {
// 'this' is the user being removed. Provide callbacks here if you want
// to be notified of the calls' result.
scanModel.remove({ user: this._id }).exec();
categoryModel.remove({ user: this._id }).exec();
next();
});
Declaration of id
var id = 3;
I want to update this object
var obj = {"comments" : {
"commentedBy" : "test",
"date" : "",
"comment" : "Hello world",
"subComments" : {
"commentedBy" : "jaril 2",
"date" : "",
"comment" : "Hello world inside dark",
"subComments" : {
"commentedBy" : "jaril 3",
"date" : "",
"comment" : "wow working great",
"subComments" : {
"commentedBy" : "jaril 4",
"date" : "",
"comment" : "wow working great",
"commentId" : 4
},
"commentId" : 3
},
"commentId" : 2
},
"commentId" : 1
},
"dueDate" : "",
"createdDate" : "",
"lastUpdated" : "",
"checkList" : [],
"position" : 2,
"status" : "active"
}
}
Function is this
function deleteCommentId(comments){
if (comments.commentId == id)){
delete comments;
return comments;
}
if (comments.subComments) {
deleteCommentId(comments.subComments);
}
return comments;
}
Function Object is this
if(id == 1){
result[0].comments = {};
} else {
deleteCommentId(obj.comments);
}
console.log("final object==>", obj);
I want output like this
{"comments" : {
"commentedBy" : "test",
"date" : "",
"comment" : "Hello world",
"subComments" : {
"commentedBy" : "jaril 2",
"date" : "",
"comment" : "Hello world inside dark",
"commentId" : 2
},
"commentId" : 1
},
"dueDate" : "",
"createdDate" : "",
"lastUpdated" : "",
"checkList" : [],
"position" : 2,
"status" : "active"
}
}
Any help would be appreciated
Note: I want to remove nested subcomments object using id if I pass
id=3 then it should remove subcomment of 2, How can I remove subcomments of 2 if id = 3
The function below should do it.
(I removed the irrelevant parts or your object to make things a bit easier to read.)
var obj = {
"comments": {
"subComments": {
"subComments": {
"subComments": {
"commentId": 4
},
"commentId": 3
},
"commentId": 2
},
"commentId": 1
}
};
function deleteCommentId(comments, id) {
if (comments.subComments) {
if (comments.subComments.commentId === id) {
delete comments.subComments;
} else {
deleteCommentId(comments.subComments, id);
}
}
}
deleteCommentId(obj.comments, 3);
console.log(obj);
var id = 3;
var obj = {"comments":{"commentedBy":"test","date":"","comment":"Hello world","subComments":{"commentedBy":"jaril 2","date":"","comment":"Hello world inside dark","subComments":{"commentedBy":"jaril 3","date":"","comment":"wow working great","subComments":{"commentedBy":"jaril 4","date":"","comment":"wow working great","commentId":4},"commentId":3},"commentId":2},"commentId":1},"dueDate":"","createdDate":"","lastUpdated":"","checkList":[],"position":2,"status":"active"}
function deleteCommentId(comments, id) {
if (comments.subComments) {
if (comments.subComments.commentId === id) {
delete comments.subComments;
} else {
deleteCommentId(comments.subComments, id);
}
}
}
deleteCommentId(obj.comments, id);
console.log("final object==>",obj);
var expectedJSON = {"comments":{"commentedBy":"test","date":"","comment":"Hello world","subComments":{"commentedBy":"jaril 2","date":"","comment":"Hello world inside dark","commentId":2},"commentId":1},"dueDate":"","createdDate":"","lastUpdated":"","checkList":[],"position":2,"status":"active"}
console.log("Output match: ",JSON.stringify(obj) == JSON.stringify(expectedJSON));
i have a javascript code that i would like to add a simple style to the buttons. Even if i have to add css please let me know where to add it and using which line. Thanks.
I have a javascript code that I would like to add a simple style to the buttons. Even if I have to add CSS please let me know where to add it and using which line. Thanks.
define(["jquery","qlik"], function($, qlik) {
return {
initialProperties: {
version: 1.0,
var_to_use: "vButtonValue",
num_of_buttons: "1",
buttonname1: " YTD ",
buttonname2: " MTD ",
buttonname3: " TODAY ",
buttonname4: " Clear ",
buttonname5: "Button 5",
buttonvalue1: "1",
buttonvalue2: "2",
buttonvalue3: "3",
buttonvalue4: "4",
buttonvalue5: "5"
},
//property panel
definition: {
type: "items",
component: "accordion",
items: {
settings: {
uses: "settings",
items : {
usevariable : {
ref : "var_to_use",
expression:"optional",
label : "Variable to use",
type : "string",
defaultValue : "vButtonValue"
},
usevariable2 : {
ref : "num_of_buttons",
expression:"",
label : "Number of buttons",
type : "integer",
component: "dropdown",
defaultValue : "1",
max:5,
options:
[ {
value: "1",
label: "1"
}, {
value: "2",
label: "2"
}, {
value: "3",
label: "3"
}, {
value: "4",
label: "4"
}, {
value: "5",
label: "5"
}],
},
usevariable4 : {
label : "Button Values",
ref : "button_value_block",
settings: {
uses: "settings",
items : {
buttonvalue1p : {
ref : "buttonvalue1",
expression:"optional",
label : "Button value 1",
type : "string",
defaultValue : "1"
},
buttonvalue2p : {
ref : "buttonvalue2",
expression:"optional",
label : "Button value 2",
type : "string",
defaultValue : "2"
},
buttonvalue3p : {
ref : "buttonvalue3",
expression:"optional",
label : "Button value 3",
type : "string",
defaultValue : "3"
},
buttonvalue4p : {
ref : "buttonvalue4",
expression:"optional",
label : "Button value 4",
type : "string",
defaultValue : "4"
},
buttonvalue5p : {
ref : "buttonvalue5",
expression:"optional",
label : "Button value 5",
type : "string",
defaultValue : "5"
}
}
}
},
usevariable3 : {
label : "Button Names",
ref : "button_names_block",
settings: {
uses: "settings",
items : {
buttonname1p : {
ref : "buttonname1",
expression:"optional",
label : "Button name 1",
type : "string",
defaultValue : "Button 1"
},
buttonname2p : {
ref : "buttonname2",
expression:"optional",
label : "Button name 2",
type : "string",
defaultValue : "Button 2"
},
buttonname3p : {
ref : "buttonname3",
expression:"optional",
label : "Button name 3",
type : "string",
defaultValue : "Button 3"
},
buttonname4p : {
ref : "buttonname4",
expression:"optional",
label : "Button name 4",
type : "string",
defaultValue : "Button 4"
},
buttonname5p : {
ref : "buttonname5",
expression:"optional",
label : "Button name 5",
type : "string",
defaultValue : "Button 5"
}
}
},
}
}
}
}
},
snapshot: {
canTakeSnapshot: false
},
paint: function ($element,layout) {
var numbuttons=layout.num_of_buttons;
var numbuttonsmax=parseFloat(numbuttons)+1;
if (numbuttonsmax > 6) {
numbuttonsmax = 6;
};
var html='<div width="100%">';
var i = 0;
var idbutnom='hola';
for (i = 1; i < numbuttonsmax; i++) {
switch(i) {
case 1:
idbutnom=layout.buttonname1;
break;
case 2:
idbutnom=layout.buttonname2;
break;
case 3:
idbutnom=layout.buttonname3;
break;
case 4:
idbutnom=layout.buttonname4;
break;
case 5:
idbutnom=layout.buttonname5;
break;
};
html=html+"<button class='qirby-button' width='auto' id='button" + i + "'>" + idbutnom + "</button>"
};
html=html+"</div>";
$element.html(html);
$element.find("#button1").on("qv-activate", function() {
var app = qlik.currApp();
var textobus=layout.var_to_use;
app.variable.setContent(textobus, layout.buttonvalue1);
});
$element.find("#button2").on("qv-activate", function() {
var app = qlik.currApp();
var textobus=layout.var_to_use;
app.variable.setContent(textobus, layout.buttonvalue2);
});
$element.find("#button3").on("qv-activate", function() {
var app = qlik.currApp();
var textobus=layout.var_to_use;
app.variable.setContent(textobus, layout.buttonvalue3);
});
$element.find("#button4").on("qv-activate", function() {
var app = qlik.currApp();
var textobus=layout.var_to_use;
app.variable.setContent(textobus, layout.buttonvalue4);
});
$element.find("#button5").on("qv-activate", function() {
var app = qlik.currApp();
var textobus=layout.var_to_use;
app.variable.setContent(textobus, layout.buttonvalue5);
});
}
}
});
I have two collections
1) Floorplan_backup.
The structure of the document looks like :-
{
"_id" : ObjectId("5877e18a88db272b578572bd"),
"floorplans" : [
{
"name" : "Campus Center - Atrium",
"uuid" : NumberLong(3),
"cameras" : []
},
{
"name" : "Campus Center - Bridge ",
"uuid" : NumberLong(4),
"cameras" : []
},
{
"name" : "Campus Center - Top",
"uuid" : NumberLong(5),
"cameras" : []
},
{
"name" : "Performance Dining Hall",
"uuid" : NumberLong(6),
"cameras" : []
},
{
"name" : "Main Kitchen",
"uuid" : NumberLong(7),
"cameras" : []
},
{
"name" : "OrgChart - Board of Trustees",
"uuid" : NumberLong(8),
"cameras" : []
},
{
"name" : "OrgChart - Academic Affairs",
"uuid" : NumberLong(9),
"cameras" : []
},
{
"name" : "OrgChart - Admissions",
"uuid" : NumberLong(10),
"cameras" : []
},
{
"name" : "OrgChart - Athletics",
"uuid" : NumberLong(11),
"cameras" : []
},
{
"name" : "OrgChart - Financial",
"uuid" : NumberLong(14),
"cameras" : []
},
{
"name" : "OrgChart - HR",
"uuid" : NumberLong(15),
"cameras" : []
},
{
"name" : "OrgChart - Institutional",
"uuid" : NumberLong(16),
"cameras" : []
},
{
"name" : "OrgChart - IT",
"uuid" : NumberLong(17),
"cameras" : []
},
{
"name" : "OrgChart - Student Success",
"uuid" : NumberLong(20),
"cameras" : []
},
{
"name" : "OrgChart - Student Life",
"uuid" : NumberLong(21),
"cameras" : []
}
],
"userid" : "user-56cb3c4c0c953470865336",
"firstviewhost" : undefined
}
2) Floorplan . The structure of the document
{
"_id" : ObjectId("589025b03422cafc09913363"),
"userid" : "user-56cb3c4c0c953470865336",
"firstviewhost" : "dean.vizsafe.com"
}
I would like to update the firstviewhost in Floorpla_backup with the value "dean.vizsafe.com" by matching the userid of both the collections.
The javascript code used is :-
db.floorplan_backup.find().forEach(function (doc1) {
var doc2 = db.floorplan.find({ userid: doc1.userid }, { firstviewhost: 1 });
if (doc2 != null) {
doc1.firstviewhost = doc2.firstviewhost;
db.floorplan_backup.save(doc1);
}
});
But is is not giving me the desired resultset . Could you let me know what is wrong with this?
doc2 is an array. Use findOne to get an object.
db.floorplan_backup.find().forEach(function (doc1) {
var doc2 = db.floorplan.findOne({ userid: doc1.userid }, { firstviewhost: 1 });
if (doc2 != null) {
doc1.firstviewhost = doc2.firstviewhost;
db.floorplan_backup.save(doc1);
}
});
find returns you a cursor, instead use findOne.
db.floorplan_backup.find().forEach(function (doc1) {
var doc2 = db.floorplan.findOne({ userid: doc1.userid }, { firstviewhost: 1 });
if (doc2 != null) {
doc1.firstviewhost = doc2.firstviewhost;
db.floorplan_backup.save(doc1);
}
});
I'm trying to programmatically create a dojo tree from a json file. The problem is that my json objects reference to their parents not children as in some examples.
Unfortunately my output looks only like this:
I have the following javascript code:
<script type="text/javascript">
require(["dijit/Tree", "dojo/data/ItemFileReadStore", "dijit/tree/ForestStoreModel", "dijit/tree/ObjectStoreModel", "dojo/domReady!"],
function(Tree, ItemFileReadStore, ObjectStoreModel, ForestStoreModel){
var store = new ItemFileReadStore({
url: "/_data/test.json",
getChildren: function(object){
return this.query({F_TopLevelCategoryID: object.P_CategoryID});
}
});
var myModel = new ObjectStoreModel({
store: store,
labelAttr:'CategoryName',
query: {"P_CategoryID": 0}
});
var myTree = new Tree({
model: myModel
}, "treeOne");
myTree.startup();
});
</script>
The json file looks like the following:
{ "identifier" : "P_CategoryID",
"label" : "CategoryName",
"items" : [ { "CategoryName" : "Category 1",
"F_TopLevelCategoryID" : 0,
"P_CategoryID" : 1
},
{ "CategoryName" : "Category 2",
"F_TopLevelCategoryID" : 1,
"P_CategoryID" : 2
},
{ "CategoryName" : "Category 3",
"F_TopLevelCategoryID" : 1,
"P_CategoryID" : 3
},
{ "CategoryName" : "Category 4",
"F_TopLevelCategoryID" : 1,
"P_CategoryID" : 4
},
{ "CategoryName" : "Category 5",
"F_TopLevelCategoryID" : 3,
"P_CategoryID" : 5
},
{ "CategoryName" : "Category 6",
"F_TopLevelCategoryID" : 4,
"P_CategoryID" : 6
},
{ "CategoryName" : "Category 7",
"F_TopLevelCategoryID" : 4,
"P_CategoryID" : 7
},
{ "CategoryName" : "Category 8",
"F_TopLevelCategoryID" : 0,
"P_CategoryID" : 8
},
{ "CategoryName" : "Top Level Category",
"P_CategoryID" : 0
}
]
}
Where is the problem?
I believe it was a fluke that you received a partial result.
You have forestStoreModel and objectStoreModel transposed in the require block, and referencing parents is fine!
Also, use dojo/store instead of ItemFileReadStore, as the latter is deprecated. Use the data parameter on Memory:
var store = new Memory({
data: testJson,
getChildren: ...
});
See my working fiddle here, using dojo/store/Memory: https://jsfiddle.net/yubp45sa/
You can get your data into the memory store using dojo/request:
request("testData.json").then(...);
http://dojotoolkit.org/reference-guide/1.10/dojo/request.html
I had another request example in previously, which requested the data through a GET from a custom nodeJS route (returning JSON):
request.get("/configInfo", {
handleAs: "json"
}).then(...);