mongodb - Select array inner element -


i have trouble mongodb data below. want data [projects][log][subject]. so, tried this

$project':{_id:0, projects.log.subject:1} 

but not correct syntax..

{     "_id": objectid("569f3a3e9d2540764d8bde59"),     "a": "book",     "server": "us",     "projects": [         {             "domainarray": [                 {                     ~~~~                 }             ],             "log": [                 {                     ~~~~~,                     "subject": "i want this"                 }             ],             "before": "234234234"         },         {             "domainarray": [                 {                     ~~~~                 }             ],             "log": [                 {                     ~~~~~,                     "subject": "i want this"                 }             ],             "before": "234234234"         },....     ] //end of projects }//end of document 

how can data group [subject]? have no idea this..

edited- expected data this

{ "subject":"first", "subject":"second", "subject":"third", "subject":"~~~" } 

is possible? want array of subject.

not sure whether expected result or not. can please try this:

db.project.aggregate([             {$project:{projects:1,_id:0}},             {$unwind:"$projects"},             {$unwind:"$projects.log"},             {$project:{subject:"$projects.log.subject",_id:0}}             ]) 

and map-reduce above result of word count below:

//map function var map = function() {    for(var in this.projects)   {       for(var j in this.projects[i].log)       {           var arraywords = this.projects[i].log[j].subject.split(" ");          for(var k = 0; k < arraywords.length; k++)          {             emit(arraywords[k],{occurance:1});           }        }     }   };  //reduce function function reduce(word, arrayoccurance) {    var totaloccurance = 0;    (var = 0; < arrayoccurance.length; i++)    {     totaloccurance = totaloccurance + arrayoccurance[i].occurance;   }    return { occurance:totaloccurance }; }  //combine both function operation , output result wordoccurance collection  db.project.mapreduce(                        map,                        reduce,                        {                           query: {"projects.log.subject":"~~~~~"},                            out: "wordoccurance"                         }                      )  //query result output  db.wordoccurance.find()  

you can change query under mapreduce match subject want word count. please let me know if mapreduce function doesn't meet expected result.

you can refer below 2 pages create , troubleshoot map , reduce function:

troubleshoot map function

troubleshoot reduce function


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -