2016-08-02 11 views
5

में एकाधिक समूह इस संग्रह को पसंद करता है।मोंगोड

{ 
"_id" : ObjectId("572c4ed33c1b5f51215219a8"), 

"name" : "This is an angular course, and integeration with php", 
"description" : "After we connected we can query or update the database just how we would using the mongo API with the exception that we use a callback. The format for callbacks is always callback(error, value) where error is null if no exception has occured. The update methods save, remove, update and findAndModify also pass the lastErrorObject as the last argument to the callback function.", 
"difficulty_level" : "Beginner", 
"type" : "Fast Track", 
"tagged_skills" : [ 
    { 
     "_id" : "5714e894e09a0f7d804b2254", 
     "name" : "PHP" 
    }, 
    { 
     "_id" : "5717355806313b1f1715fa50", 
     "name" : "c++" 
    }, 
    { 
     "_id" : "5715025bc2c5dbb4675180da", 
     "name" : "java" 
    }, 
    { 
     "_id" : "5714f188ec325f5359979e33", 
     "name" : "symphony" 
    } 
]} 

मैं संग्रह, कठिनाई के स्तर और टैग किए गए कौशल के आधार पर संग्रह द्वारा समूह करना चाहता हूं और एक प्रश्न में भी गिनती प्राप्त करना चाहता हूं।

मैं कौशल गणना जोड़ने में सक्षम नहीं हूं।

मेरे प्रश्न इस प्रकार है: -

db.course.aggregate([ 
{$unwind:"$tagged_skills"}, 
{$group:{ 
    _id:null, 
skills: { $addToSet: "$tagged_skills.name" }, 
Normal_df:{$sum:{ 
       "$cond": [ 
        { "$eq":[ "$difficulty_level","Normal"] }, 
        1, 
        0 
       ] 
      }}, 
Beginner_df:{$sum:{ 
       "$cond": [ 
        { "$eq":[ "$difficulty_level","Beginner"] }, 
        1, 
        0 
       ] 
      }}, 
Intermediate_df:{$sum:{ 
       "$cond": [ 
        { "$eq":[ "$difficulty_level","Intermediate"] }, 
        1, 
        0 
       ] 
      }}, 
Advanced_df:{$sum:{ 
       "$cond": [ 
        { "$eq":[ "$difficulty_level","Advanced"] }, 
        1, 
        0 
       ] 
      }}, 
Fast_Track_type:{$sum:{ 
       "$cond": [ 
        { "$eq":[ "$type","Fast Track"] }, 
        1, 
        0 
       ] 
      }}, 
Normal_type:{$sum:{ 
       "$cond": [ 
        { "$eq":[ "$type","Normal"] }, 
        1, 
        0 
       ] 
      }}, 
Beginner_type:{$sum:{ 
       "$cond": [ 
        { "$eq":[ "$type","Beginner"] }, 
        1, 
        0 
       ] 
      }}, 
Normal_Track_type:{$sum:{ 
       "$cond": [ 
        { "$eq":[ "$type","Normal Track"] }, 
        1, 
        0 
       ] 
      }}, 
    }} 
]) 

परिणाम इस प्रकार है: -

{ 
    "_id" : null, 

    "skills" : [ 
     "SQL", 
     "PHP", 
     "java", 
     "Angular Js", 
     "Laravel 23", 
     "c++", 
     "Node Js", 
     "symphony", 
     "Mysql", 
     "Express Js", 
     "JAVA" 
    ], 
    "Normal_df" : 1, 
    "Beginner_df" : 14, 
    "Intermediate_df" : 7, 
    "Advanced_df" : 2, 
    "Fast_Track_type" : 8, 
    "Normal_type" : 6, 
    "Beginner_type" : 1, 
    "Normal_Track_type" : 9 
} 

मैं भी उनकी गिनती के साथ सभी कौशल प्राप्त करना चाहते हैं।

उत्तर

2

उनकी गणना के साथ सभी कौशल प्राप्त करने के लिए, आपको पहले सभी कौशल की एक सूची प्राप्त करने की आवश्यकता है। आप इस सूची को distinct आउटलुक फ़ील्ड पर कमांड के साथ प्राप्त कर सकते हैं। इस सूची के साथ आप फिर उचित $group पाइपलाइन दस्तावेज है कि $sum और $cond ऑपरेटरों का उपयोग करेगा निर्माण कर सकते हैं।

निम्नलिखित उपयोग के मामले पर विचार करें:

var difficultyLevels = db.course.distinct("difficulty_level"), 
    types = db.course.distinct("type"), 
    skills = db.course.distinct("tagged_skills.name"), 
    unwindOperator = { "$unwind": "$tagged_skills" }, 
    groupOperator = { 
     "$group": { 
      "_id": null, 
      "skills": { "$addToSet": "$tagged_skills.name" } 
     }  
    }; 

difficultyLevels.forEach(function (df){ 
    groupOperator["$group"][df+"_df"] = { 
     "$sum": { 
      "$cond": [ { "$eq": ["$difficulty_level", df] }, 1, 0] 
     } 
    } 
}); 

types.forEach(function (type){ 
    groupOperator["$group"][type.replace(" ", "_")+"_type"] = { 
     "$sum": { 
      "$cond": [ { "$eq": ["$type", type] }, 1, 0] 
     } 
    } 
}); 

skills.forEach(function (skill){ 
    groupOperator["$group"][skill] = { 
     "$sum": { 
      "$cond": [ { "$eq": ["$tagged_skills.name", skill] }, 1, 0] 
     } 
    } 
}); 

//printjson(groupOperator); 
db.course.aggregate([unwindOperator, groupOperator]); 

पहली पंक्ति में, हम difficulty_level मैदान पर distinct आदेश चलाकर कठिनाई स्तर के साथ एक सरणी प्राप्त

db.course.distinct("difficulty_level") 

यह सरणी

का उत्पादन करेगा
var difficultyLevels = ["Normal", "Beginner", "Intermediate", "Advanced"] 

इसी तरह, पिछले distinct संचालन उस कुंजी के संभावित अद्वितीय मूल्यों की सूची लौटाएंगे।

इन सूचियों मिलने के बाद, आप तो पाइपलाइन वस्तुओं forEach() पद्धति का उपयोग करके सूची में प्रत्येक दिए गए आइटम के दस्तावेज़ कुंजी पॉप्युलेट करने के लिए बना सकते हैं। इसके बाद आप परिणामस्वरूप दस्तावेज़ का उपयोग कर सकते हैं, जो इस

printjson(groupOperator); 
{ 
    "$group" : { 
     "_id" : null, 
     "skills" : { 
      "$addToSet" : "$tagged_skills.name" 
     }, 
     "Beginner_df" : { 
      "$sum" : { 
       "$cond" : [ 
        { 
         "$eq" : [ 
          "$difficulty_level", 
          "Beginner" 
         ] 
        }, 
        1, 
        0 
       ] 
      } 
     }, 
     "Intermediate_df" : { 
      "$sum" : { 
       "$cond" : [ 
        { 
         "$eq" : [ 
          "$difficulty_level", 
          "Intermediate" 
         ] 
        }, 
        1, 
        0 
       ] 
      } 
     }, 
     "Fast_Track_type" : { 
      "$sum" : { 
       "$cond" : [ 
        { 
         "$eq" : [ 
          "$type", 
          "Fast Track" 
         ] 
        }, 
        1, 
        0 
       ] 
      } 
     }, 
     "PHP" : { 
      "$sum" : { 
       "$cond" : [ 
        { 
         "$eq" : [ 
          "$tagged_skills.name", 
          "PHP" 
         ] 
        }, 
        1, 
        0 
       ] 
      } 
     }, 
     "c++" : { 
      "$sum" : { 
       "$cond" : [ 
        { 
         "$eq" : [ 
          "$tagged_skills.name", 
          "c++" 
         ] 
        }, 
        1, 
        0 
       ] 
      } 
     }, 
     "java" : { 
      "$sum" : { 
       "$cond" : [ 
        { 
         "$eq" : [ 
          "$tagged_skills.name", 
          "java" 
         ] 
        }, 
        1, 
        0 
       ] 
      } 
     }, 
     "symphony" : { 
      "$sum" : { 
       "$cond" : [ 
        { 
         "$eq" : [ 
          "$tagged_skills.name", 
          "symphony" 
         ] 
        }, 
        1, 
        0 
       ] 
      } 
     }, 
     "C#" : { 
      "$sum" : { 
       "$cond" : [ 
        { 
         "$eq" : [ 
          "$tagged_skills.name", 
          "C#" 
         ] 
        }, 
        1, 
        0 
       ] 
      } 
     }, 
     "Scala" : { 
      "$sum" : { 
       "$cond" : [ 
        { 
         "$eq" : [ 
          "$tagged_skills.name", 
          "Scala" 
         ] 
        }, 
        1, 
        0 
       ] 
      } 
     }, 
     "javascript" : { 
      "$sum" : { 
       "$cond" : [ 
        { 
         "$eq" : [ 
          "$tagged_skills.name", 
          "javascript" 
         ] 
        }, 
        1, 
        0 
       ] 
      } 
     } 
    } 
} 
+1

धन्यवाद यह ठीक काम कर रहा है। –

संबंधित मुद्दे