Here is my database structure
{
"_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
}
From the question array, I want to get a random object (random question) each time I fire the query.
I don't want to collect all the objects at a time and manupulate them in node. Is it possible to write a query so that it will return a random object each time?
Try this logic
1) use $unwind on the array "questions", if you use includeArrayIndex, it will create documents with index see examples in unwind documentation
2) After unwinding the array pass a random number to retrieve a question
You need to de-normalize the "questions" array using the $unwind
operator. From there you can use the $sample
pipeline operator to return a random document (random question).
db.collection.aggregate(
[
{ "$unwind": "$questionLibrary.questions" },
{ "$sample": { "size": 1 } }
]
)
But I suggest you change your document structure and save "questions" into it own collection because $unwind
can produce a very large result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With