1

I want to get random question from the list, and i want to sort randomly the option, i tried and i get the random question, but i don`t know how to sort randomly from the array.

this is my sample

{ 'question': 'question 1...', 'option': [{ 'id': '1', 'text': 'opt1' }, { 'id': '2', 'text': 'opt2' }, { 'id': '3', 'text': 'opt3' }] },
{ 'question': 'question 2...', 'option': [{ 'id': '1', 'text': 'opt1' }, { 'id': '2', 'text': 'opt2' }, { 'id': '3', 'text': 'opt3' }, { 'id': '4', 'text': 'opt4' }, { 'id': '5', 'text': 'opt5' }] },
{ 'question': 'question 3...', 'option': [{ 'id': '1', 'text': 'opt1' }, { 'id': '2', 'text': 'opt2' }, { 'id': '3', 'text': 'opt3' }] },
{ 'question': 'question 4...', 'option': [{ 'id': '1', 'text': 'opt1' }, { 'id': '2', 'text': 'opt2' }, { 'id': '3', 'text': 'opt3' }, { 'id': '4', 'text': 'opt4' }] },
{ 'question': 'question 5...', 'option': [{ 'id': '1', 'text': 'opt1' }, { 'id': '2', 'text': 'opt2' }, { 'id': '3', 'text': 'opt3' }] },
db.getCollection('collection').aggregate([
    {
        "$sample": { "size": 3 }
    },
    {
        "$addFields": {
                "option_count": {"$size": "$option"}       
        },
    }
])

i want to get this result

{ 'question': 'question 2...', 'option': [{ 'id': '2', 'text': 'opt2' }, { 'id': '3', 'text': 'opt3' }, { 'id': '1', 'text': 'opt1' }, { 'id': '5', 'text': 'opt5' }, { 'id': '4', 'text': 'opt4' }] },
{ 'question': 'question 5...', 'option': [{ 'id': '3', 'text': 'opt3' }, { 'id': '1', 'text': 'opt1' }, { 'id': '2', 'text': 'opt2' } ] },
{ 'question': 'question 4...', 'option': [{ 'id': '2', 'text': 'opt2' }, { 'id': '3', 'text': 'opt3' }, { 'id': '1', 'text': 'opt1' },   { 'id': '4', 'text': 'opt4' }] },
2
  • 1
    Interesting question - not sure if this is possible throgh mongodb, so I'd just use $sample to get random question documents and then shuffle the options on the client-side. Commented Jan 12, 2022 at 9:18
  • @eol I have the same idea as you because all options are required. It's a good idea to shuffle options on the client side. Commented Jan 13, 2022 at 0:55

1 Answer 1

0

Use $function New in version 4.4.

shuffle

db.collection.aggregate([
  {
    "$sample": {
      "size": 3
    }
  },
  {
    "$set": {
      "option": {
        "$function": {
          "body": "function (arr) {return arr.sort( () => Math.random()-0.5) ;}",
          "args": [
            "$option"
          ],
          "lang": "js"
        }
      }
    }
  }
])

mongoplayground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.