'How to call the desired collection from Mongobd?

I am a beginner. I making a quiz. I stored my data in a few collections in MongoDB. How to call a specific collection from MongoDB? I know how to call only one.

```export async function getStaticProps () {
   const { db } = await connectToDatabase()

   const questions = await db
   .collection('questions')
   .find({})
   .sort({ metacritic: -1 })
   .limit(20)
   .toArray()

   return {
   props: {
  questions: JSON.parse(JSON.stringify(shuffleArray(questions)))
   }
  }
}


Solution 1:[1]

export async function getStaticProps (req, res) {
    // this is the collection we pass in our query parameters then is 
    // destructured here and I rename it to COLLECTION just so we can see where I 
    // pass it below
    const { collection: COLLECTION } = req.params

    // this is the db connection 
    const { db } = await connectToDatabase()
        
    const data = await db

    // here we pass the COLLECTION
           .collection(COLLECTION)
           .find({})
           .sort({ metacritic: -1 })
           .limit(20)
           .toArray()

//here you should handle how you want the data structured and then depending on that have the results sent with a status code of 200 if all is well
           return {
           props: {
             data: JSON.parse(JSON.stringify(shuffleArray(data)))
           }
          }
        }

example of query endpoint 

http://someconnection/:collection
which will be 
http://someconnection/questions or http://someconnection/answers or http://someconnection/users

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marc_s