2016-05-10 5 views
1

Meine JSON-Datei sieht wie folgt aus:mongodb verschachtelte Array Anpassungs- und Sortier

{ 
    "_id": ObjectId("57208fa10f80d14e9472942f"), 
    "title": "How to stop hair loss?", 
    "postdatetime": "Wed Apr 27 2016 15:38:33 GMT+0530 (IST)", 
    "status": "Active", 
    "answer": [{ 
     "answerid": ObjectId("5719b83ffa86ca75d25b600e"), 
     "text": "l eat protien", 
     "imageurl": "http://leavesugar.jpg", 
     "videourl": "http://leavesugar.mp4", 
     "author": { 
      "userid": ObjectId("5719b83ffa86ca75d25b600c"), 
      "name": "chetan kumar", 
      "imageurl": "http://chetan.jpg", 
      "followers": 150 
     }, 
     "postdatetime": "Wed Apr 27 2016 11:55:45 GMT+0530 (IST)", 
     "status": "Active", 
     "comment": [{ 
      "commentid": ObjectId("5719b83ffa86ca75d25b600c"), 
      "parentid": ObjectId("5719b83ffa86ca75d25b600e"), 
      "posttext": "Most important is workout", 
      "author": { 
       "userid": ObjectId("5719b83ffa86ca75d25b600c"), 
       "name": "raju", 
       "imageurl": "http://raju.jpg", 
      }, 
      "postdatetime": "Wed Apr 27 2016 12:17:59 GMT+0530 (IST)" 
     }] 
    }, { 
     "answerid": ObjectId("5719b83ffa86ca75d25b600f"), 
     "text": "WORKOUT 1 TO 2 HRS DAILY", 
     "imageurl": "http://workout.jpg", 
     "videourl": "http://workout.mp4", 
     "author": { 
      "userid": ObjectId("5719b83ffa86ca75d25b600a"), 
      "name": "Ram kumar", 
      "imageurl": "http://ram.jpg", 
     }, 
     "postdatetime": "Wed Apr 27 2016 11:56:26 GMT+0530 (IST)", 
     "status": "Active", 
     "comment": [{ 
      "commentid": ObjectId("5719b83ffa86ca75d25b600e"), 
      "parentid": ObjectId("5719b83ffa86ca75d25b600f"), 
      "posttext": "EAT MORE PROTIEN", 
      "author": { 
       "userid": ObjectId("5719b83ffa86ca75d25b600e"), 
       "name": "raju", 
       "imageurl": "http://raju.jpg", 
      }, 
      "postdatetime": "Wed Apr 27 2016 12:49:24 GMT+0530 (IST)" 
     }, { 
      "commentid": ObjectId("5719b83ffa86ca75d25b600f"), 
      "parentid": ObjectId("5719b83ffa86ca75d25b600f"), 
      "posttext": "EAT PROTIEN AND WORKOUT DAILY", 
      "author": { 
       "userid": ObjectId("5719b83ffa86ca75d25b600f"), 
       "name": "RAMU", 
       "imageurl": "http://RAMU.jpg", 
      }, 
      "postdatetime": "Wed Apr 27 2016 12:50:07 GMT+0530 (IST)" 
     }] 
    }] 
} { 
    "_id": ObjectId("57204c194694965a19bac484"), 
    "title": "How to loose weight?", 
    "postdatetime": "Wed Apr 27 2016 10:50:25 GMT+0530 (IST)", 
    "status": "Active", 
    "answer": [{ 
     "answerid": ObjectId("5719b83ffa86ca75d25b600b"), 
     "text": "leave sugar and eat protien", 
     "imageurl": "http://leavesugar.jpg", 
     "videourl": "http://leavesugar.mp4", 
     "author": { 
      "userid": ObjectId("5719b83ffa86ca75d25b600c"), 
      "name": "chetan kumar", 
      "imageurl": "http://chetan.jpg", 
      "followers": 250 
     }, 
     "postdatetime": "Wed Apr 27 2016 11:55:45 GMT+0530 (IST)", 
     "status": "InActive", 
     "comment": [{ 
      "commentid": ObjectId("5719b83ffa86ca75d25b600c"), 
      "parentid": ObjectId("5719b83ffa86ca75d25b600b"), 
      "posttext": "Most important is workout", 
      "author": { 
       "userid": ObjectId("5719b83ffa86ca75d25b600c"), 
       "name": "raju", 
       "imageurl": "http://raju.jpg", 
      }, 
      "postdatetime": "Wed Apr 27 2016 12:17:59 GMT+0530 (IST)" 
     }] 
    }, { 
     "answerid": ObjectId("5719b83ffa86ca75d25b600a"), 
     "text": "WORKOUT 1 TO 2 HRS DAILY", 
     "imageurl": "http://workout.jpg", 
     "videourl": "http://workout.mp4", 
     "author": { 
      "userid": ObjectId("5719b83ffa86ca75d25b600a"), 
      "name": "Ram kumar", 
      "imageurl": "http://ram.jpg", 
     }, 
     "postdatetime": "Wed Apr 27 2016 11:56:26 GMT+0530 (IST)", 
     "status": "Active", 
     "comment": [{ 
      "commentid": ObjectId("5719b83ffa86ca75d25b600e"), 
      "parentid": ObjectId("5719b83ffa86ca75d25b600a"), 
      "posttext": "EAT MORE PROTIEN", 
      "author": { 
       "userid": ObjectId("5719b83ffa86ca75d25b600e"), 
       "name": "raju", 
       "imageurl": "http://raju.jpg" 
      }, 
      "postdatetime": "Wed Apr 27 2016 12:49:24 GMT+0530 (IST)" 
     }, { 
      "commentid": ObjectId("5719b83ffa86ca75d25b600f"), 
      "parentid": ObjectId("5719b83ffa86ca75d25b600a"), 
      "posttext": "EAT PROTIEN AND WORKOUT DAILY", 
      "author": { 
       "userid": ObjectId("5719b83ffa86ca75d25b600f"), 
       "name": "RAMU", 
       "imageurl": "http://RAMU.jpg" 
      }, 
      "postdatetime": "Wed Apr 27 2016 12:50:07 GMT+0530 (IST)" 
     }] 
    }, { 
     "answerid": ObjectId("5719b83ffa86ca75d25b600d"), 
     "text": "WORKOUT 1 TO 2 HRS DAILY", 
     "imageurl": "http://workout.jpg", 
     "videourl": "http://workout.mp4", 
     "author": { 
      "userid": ObjectId("5719b83ffa86ca75d25b600a"), 
      "name": "Ram kumar", 
      "imageurl": "http://ram.jpg" 
     }, 
     "postdatetime": "Thu Apr 28 2016 10:35:38 GMT+0530 (IST)", 
     "status": "Active", 
     "comment": [] 
    }] 
} 

ich will „Status“, um herauszufinden: „Aktiv“, „answer.status“: „Aktiv“ und kurze die Antwort entsprechend ihrer maximalen Nr. von Kommentar

Ich versuche dies nur „status“ und „answer.status“, um herauszufinden, aktiv ist, aber es alle Ergebnis dessen „answer.status“ zeigen aktiv oder inaktiv

Wie Sie dieses Problem lösen können, bitte helfen Sie mir.

benutzte ich dieses Aggregat alle "Status", um herauszufinden, und "answer.status": "Aktiv"

db.QuestionAnswer.aggregate([{ 
      "$match" : { 
       $and : [{ 
         "status" : "Active" 
        }, { 
         "answer.status" : "Active" 
        } 
       ] 
      } 
     } 
    ]).pretty() 
+0

Sie müssen ag verwenden gregation. Es gibt eine einfache Aggregationsabfrage, wenn Sie mongodb verwenden 3.2 –

+0

Ich benutze diese db.QuestionAnswer.aggregate ([{"$ match": {$ und: [{"status": "Active"}, {"answer.status"): "Aktiv"}]}}]). Pretty() –

+0

Bitte geben Sie das erwartete Dokument Beispiel – profesor79

Antwort

1

nur aktive Antworten verwenden Sie bitte Abfrage unten aufgeführt zu erhalten:

db.aaa.aggregate([{ 
      $match : { 
       status : "Active" 
      } 
     }, { 
      $project : { 
       _id : 1, 
       title : 1, 
       postdatetime : 1, 
       status : 1, 
       answer : { 
        $filter : { 
         input : "$answer", 
         as : "answe", 
         cond : { 
          $eq : ["$$answe.status", "Active"] 
         } 
        } 
       } 
      } 
     } 
    ]) 

Ausgang

{ 
    "_id" : ObjectId("57208fa10f80d14e9472942f"), 
    "title" : "How to stop hair loss?", 
    "postdatetime" : "Wed Apr 27 2016 15:38:33 GMT+0530 (IST)", 
    "status" : "Active", 
    "answer" : [ 
     { 
      "answerid" : ObjectId("5719b83ffa86ca75d25b600e"), 
      "text" : "l eat protien", 
      "imageurl" : "http://leavesugar.jpg", 
      "videourl" : "http://leavesugar.mp4", 
      "author" : { 
       "userid" : ObjectId("5719b83ffa86ca75d25b600c"), 
       "name" : "chetan kumar", 
       "imageurl" : "http://chetan.jpg", 
       "followers" : 150 
      }, 
      "postdatetime" : "Wed Apr 27 2016 11:55:45 GMT+0530 (IST)", 
      "status" : "Active", 
      "comment" : [ 
       { 
        "commentid" : ObjectId("5719b83ffa86ca75d25b600c"), 
        "parentid" : ObjectId("5719b83ffa86ca75d25b600e"), 
        "posttext" : "Most important is workout", 
        "author" : { 
         "userid" : ObjectId("5719b83ffa86ca75d25b600c"), 
         "name" : "raju", 
         "imageurl" : "http://raju.jpg" 
        }, 
        "postdatetime" : "Wed Apr 27 2016 12:17:59 GMT+0530 (IST)" 
       } 
      ] 
     }, 
     { 
      "answerid" : ObjectId("5719b83ffa86ca75d25b600f"), 
      "text" : "WORKOUT 1 TO 2 HRS DAILY", 
      "imageurl" : "http://workout.jpg", 
      "videourl" : "http://workout.mp4", 
      "author" : { 
       "userid" : ObjectId("5719b83ffa86ca75d25b600a"), 
       "name" : "Ram kumar", 
       "imageurl" : "http://ram.jpg" 
      }, 
      "postdatetime" : "Wed Apr 27 2016 11:56:26 GMT+0530 (IST)", 
      "status" : "Active", 
      "comment" : [ 
       { 
        "commentid" : ObjectId("5719b83ffa86ca75d25b600e"), 
        "parentid" : ObjectId("5719b83ffa86ca75d25b600f"), 
        "posttext" : "EAT MORE PROTIEN", 
        "author" : { 
         "userid" : ObjectId("5719b83ffa86ca75d25b600e"), 
         "name" : "raju", 
         "imageurl" : "http://raju.jpg" 
        }, 
        "postdatetime" : "Wed Apr 27 2016 12:49:24 GMT+0530 (IST)" 
       }, 
       { 
        "commentid" : ObjectId("5719b83ffa86ca75d25b600f"), 
        "parentid" : ObjectId("5719b83ffa86ca75d25b600f"), 
        "posttext" : "EAT PROTIEN AND WORKOUT DAILY", 
        "author" : { 
         "userid" : ObjectId("5719b83ffa86ca75d25b600f"), 
         "name" : "RAMU", 
         "imageurl" : "http://RAMU.jpg" 
        }, 
        "postdatetime" : "Wed Apr 27 2016 12:50:07 GMT+0530 (IST)" 
       } 
      ] 
     } 
    ] 
} 
+0

Danke, dass Sie mir geholfen haben –

Verwandte Themen