我试图在一个名为“模板”的集合中获取 2 个随机文档,但我只得到一个内部服务器错误。
我已经在 mongo shell 中成功测试了以下代码:
db.templates.aggregate([{"$sample":{"size":2}}])
fastify
语法会不会有问题?
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'
我收到内部服务器错误,需要 2 个随机文档。