I am trying to get 2 random documents within a collection called 'templates' but I only get an Internal Server Error.
I have successfully tested the following code in the mongo shell:
db.templates.aggregate([{"$sample":{"size":2}}])
Could there be a problem with the fastify
syntax?
module.exports = async fastify => {
fastify.get(
'/',
{
schema: {
response: {
200: {
type: 'object',
items: {
type: 'object',
properties: {
_id: {
type: 'string'
}
}
}
}
}
}
},
async (req, res) => {
try {
const result = this.mongo.db
.collection('templates')
.aggregate([{ $sample: { size: 2 } }])
return await result
} catch (err) {
res.internalServerError()
}
}
)
}
module.exports.autoPrefix = '/questions'
I am getting an Internal Server Error, expecting 2 random documents.