MongoDB
 sql >> डेटाबेस >  >> NoSQL >> MongoDB

MongoDB नेस्टेड ऑब्जेक्ट एग्रीगेशन काउंटिंग

आपको $unwind को प्रोसेस करना होगा सरणियों के साथ काम करते समय, और आपको इसे तीन बार करने की आवश्यकता है:

 db.collection.aggregate([

     // Un-wind the array's to access filtering 
     { "$unwind": "$studies" },
     { "$unwind": "$studies.samples" },
     { "$unwind": "$studies.samples.formdata" },

     // Group results to obtain the matched count per key
     { "$group": {
         "_id": "$studies.samples.formdata.GT",
         "count": { "$sum": 1 }
     }}
 ])

आदर्श रूप से आप अपना इनपुट फ़िल्टर करना चाहते हैं। संभवतः इसे $match के साथ करें $unwind के पहले और बाद में दोनों को संसाधित किया जाता है और एक $regex उन दस्तावेज़ों से मिलान करने के लिए जहां बिंदु पर डेटा "1" से शुरू होता है।

 db.collection.aggregate([

     // Match first to exclude documents where this is not present in any array member
     { "$match": { "studies.samples.formdata.GT": /^1/ } },

     // Un-wind the array's to access filtering 
     { "$unwind": "$studies" },
     { "$unwind": "$studies.samples" },
     { "$unwind": "$studies.samples.formdata" },

     // Match to filter
     { "$match": { "studies.samples.formdata.GT": /^1/ } },

     // Group results to obtain the matched count per key
     { "$group": {
         "_id": {
              "_id": "$_id",
              "key": "$studies.samples.formdata.GT"
         },
         "count": { "$sum": 1 }
     }}
 ])

ध्यान दें कि सभी मामलों में "डॉलर $" उपसर्ग प्रविष्टियां दस्तावेज़ के गुणों का संदर्भ देने वाले "चर" हैं। दाईं ओर इनपुट का उपयोग करने के लिए ये "मान" हैं। बाईं ओर "कुंजी" को एक सादे स्ट्रिंग कुंजी के रूप में निर्दिष्ट किया जाना चाहिए। किसी कुंजी को नाम देने के लिए किसी चर का उपयोग नहीं किया जा सकता है।




  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. मोंगोडब परिणाम जावा से _id हटाएं

  2. मोंगोडब खोज शार्डिंग कुंजी बहुत धीमी है

  3. MongoDB में कंसोल कैसे साफ़ करें

  4. लाइनोड 512 वीपीएस पर मोंगोडीबी चलाने की व्यवहार्यता?

  5. mongorestore समस्या:स्कीमा संस्करण 1 वाले उपयोगकर्ताओं को सर्वर संस्करण 2.5.4 या इससे अधिक वाले सिस्टम में पुनर्स्थापित नहीं कर सकता