2016-06-16 6 views
5

Hier meine Datenbankstruktur istGet zufälliges Element aus dem Array in MongoDB

{ 
    "_id" : ObjectId("576155226d1d298c2cc3edca"), 
    "questionLibrary" : { 
      "technologyName" : "CSS", 
      "questions" : [ 
        { 
          "correctanswer" : { 
            "A1" : "CSS1" 
          }, 
          "answeroption" : { 
            "A4" : "CSS1", 
            "A3" : "CSS1", 
            "A2" : "CSS1", 
            "A1" : "CSS1" 
          }, 
          "level" : "Amature", 
          "tags" : "CSS", 
          "question" : "CSS1" 
        }, 
        { 
          "question" : "CSS2", 
          "tags" : "CSS", 
          "answeroption" : { 
            "A1" : "CSS2", 
            "A2" : "CSS2", 
            "A3" : "CSS2", 
            "A4" : "CSS2" 
          }, 
          "level" : "Amature", 
          "correctanswer" : { 
            "A1" : "CSS2" 
          } 
        }, 
        { 
          "correctanswer" : { 
            "A1" : "CSS3" 
          }, 
          "answeroption" : { 
            "A4" : "CSS3", 
            "A3" : "CSS3", 
            "A2" : "CSS3", 
            "A1" : "CSS3" 
          }, 
          "level" : "Amature", 
          "tags" : "CSS", 
          "question" : "CSS3" 
        }, 
        { 
          "correctanswer" : { 
            "A1" : "CSS4" 
          }, 
          "answeroption" : { 
            "A4" : "CSS4", 
            "A3" : "CSS4", 
            "A2" : "CSS4", 
            "A1" : "CSS4" 
          }, 
          "level" : "Amature", 
          "tags" : "CSS", 
          "question" : "CSS4" 
        }, 
        { 
          "correctanswer" : { 
            "A1" : "CSS5" 
          }, 
          "answeroption" : { 
            "A4" : "CSS5", 
            "A3" : "CSS5", 
            "A2" : "CSS5", 
            "A1" : "CSS5" 
          }, 
          "level" : "Amature", 
          "tags" : "CSS", 
          "question" : "CSS5" 
        }, 
        { 
          "correctanswer" : { 
            "A1" : "CSS6" 
          }, 
          "answeroption" : { 
            "A4" : "CSS6", 
            "A3" : "CSS6", 
            "A2" : "CSS6", 
            "A1" : "CSS6" 
          }, 
          "level" : "Amature", 
          "tags" : "CSS", 
          "question" : "CSS6" 
        }, 
        { 
          "correctanswer" : { 
            "A1" : "CSS7" 
          }, 
          "answeroption" : { 
            "A4" : "CSS7", 
            "A3" : "CSS7", 
            "A2" : "CSS7", 
            "A1" : "CSS7" 
          }, 
          "level" : "Amature", 
          "tags" : "CSS", 
          "question" : "CSS7" 
        }, 
        { 
          "correctanswer" : { 
            "A1" : "CSS8" 
          }, 
          "answeroption" : { 
            "A4" : "CSS8", 
            "A3" : "CSS8", 
            "A2" : "CSS8", 
            "A1" : "CSS8" 
          }, 
          "level" : "Amature", 
          "tags" : "CSS", 
          "question" : "CSS8" 
        }, 
        { 
          "correctanswer" : { 
            "A1" : "CSS9" 
          }, 
          "answeroption" : { 
            "A4" : "CSS9", 
            "A3" : "CSS9", 
            "A2" : "CSS9", 
            "A1" : "CSS9" 
          }, 
          "level" : "Amature", 
          "tags" : "CSS", 
          "question" : "CSS9" 
        }, 
        { 
          "correctanswer" : { 
            "A1" : "CSS10" 
          }, 
          "answeroption" : { 
            "A4" : "CSS10", 
            "A3" : "CSS10", 
            "A2" : "CSS10", 
            "A1" : "CSS10" 
          }, 
          "level" : "Amature", 
          "tags" : "CSS", 
          "question" : "CSS10" 
        } 
      ] 
    }, 
    "__v" : 3 
} 

Von der Frage Array, mag ich ein zufälliges Objekt erhalten (zufällige Frage) jedes Mal, wenn ich die Abfrage ausgelöst.

Ich möchte nicht alle Objekte gleichzeitig sammeln und sie in Knoten manupulieren. Ist es möglich, eine Abfrage zu schreiben, so dass jedes Mal ein zufälliges Objekt zurückgegeben wird? siehe examples in Abroller Dokumentation

+2

Würden vorschlagen, Ihr Schema umzustellen, um das 'Fragen'-Array zu denormalisieren, indem Sie die Daten in einer separaten Sammlung speichern, sagen Sie' Fragen'. Sie können dann den Operator [** '$ sample' **] (https://docs.mongodb.com/master/reference/operator/aggregation/sample/#pipe._S_sample) der Aggregationspipeline problemlos zusammen mit einem Filter verwenden das Tag, um ein Beispielfragendokument zu zeichnen. Es ist viel einfacher, auf diese Weise abzufragen. Eine Beispielabfrage wäre 'db.questions.aggregate ([{" $ sample ": {" size ": 1}}])' – chridam

Antwort

1

Versuchen Sie, diese Logik

1) verwenden $unwind auf dem Array "Fragen", wenn Sie includeArrayIndex verwenden, wird es Dokumente mit Index erstellen

2) Nach dem Abwickeln der Array ein Pass Zufallszahl zum Abrufen einer Frage

1

Sie müssen das "Fragen" -Array mithilfe des Operators $unwind de-normalisieren. Von dort können Sie den Pipelineoperator $sample verwenden, um ein zufälliges Dokument (zufällige Frage) zurückzugeben.

db.collection.aggregate(
    [ 
     { "$unwind": "$questionLibrary.questions" }, 
     { "$sample": { "size": 1 } } 
    ] 
) 

Aber ich schlage vor, Sie Ihre Dokumentstruktur ändern und „Fragen“ in seine eigene Sammlung speichern, weil $unwind ein sehr großes Ergebnis produzieren kann.

Verwandte Themen