javascript - 使用某些过滤器检索 n 个随机文档 MongoDB
问题描述
我需要根据给定的限制从一组随机文档中检索。如果提供了一些过滤器,则应添加它们以过滤响应的结果。我可以根据提供的字段来构建匹配和大小,但即使我有 20 个符合过滤器的文档,当我拨打电话时,我也只收到 2 或 3 个文档,我似乎无法弄清楚。如果我只设置限制,它确实会根据限制返回 N 个随机文档,但如果我添加一个过滤器,它不会给我想要的结果。
这就是我现在所做的
const limit = Number(req.query.limit || 1);
const difficulty = req.query.difficulty;
const category = req.query.category;
const settings = [
{
$sample: {
size: limit
}
}
];
if (difficulty && category) {
settings.push({
$match: {
difficulty: difficulty,
category: category
}
});
} else if (difficulty && category == null) {
settings.push({
$match: {
difficulty
}
});
}
if (difficulty == null && category) {
settings.push({
$match: {
category
}
});
}
console.log(settings);
Question.aggregate(settings)
.then(docs => {
const response = {
count: docs.length,
difficulty: difficulty ? difficulty : "random",
questions:
docs.length > 0
? docs.map(question => {
return {
_id: question._id,
question: question.question,
answers: question.answers,
difficulty: question.difficulty,
category: question.category,
request: {
type: "GET",
url:
req.protocol +
"://" +
req.get("host") +
"/questions/" +
question._id
}
};
})
: {
message: "No results found"
}
};
res.status(200).json(response);
})
.catch(err => {
res.status(500).json({
error: err
});
});
解决方案
阶段的顺序在这里很重要。您正在推动$match
阶段之后的$sample
阶段,首先将整个文件放入$size
整个文件,然后将$match
阶段应用于文件文件。$sample
d
所以最后你需要在$sample
舞台之后推动$match
舞台。顺序应该是
const limit = Number(req.query.limit || 1);
const difficulty = req.query.difficulty;
const category = req.query.category;
const settings = []
if (difficulty && category) {
settings.push({
$match: {
difficulty: difficulty,
category: category
}
})
} else if (difficulty && category == null) {
settings.push({
$match: {
difficulty
}
})
}
if (difficulty == null && category) {
settings.push({
$match: {
category
}
})
}
setting.push({
$sample: {
size: limit
}
})
console.log(settings);
Question.aggregate(settings)
推荐阅读
- r - 在 postgres 数据库中使用 R 进行多行和列更新
- typescript - 强制openapi生成器生成一个类
- reactjs - 是否可以通过 useCallback 避免自定义 React Hook 上的“eslint(react-hooks/exhaustive-deps)”错误?
- java - 使用命令行运行 Java 类
- spring - Spring MVC:Bean 名称“标记”的 BindingResult 和普通目标对象都不能用作请求属性。尝试了所有解决方案
- excel - 如何将数据范围复制到另一个工作表的最后一行?
- html - 如何定位父级内的所有标签
- activemq-artemis - # 通过 MQTT 订阅会导致连接丢失时出现巨大的队列
- c# - 创建文件有效,但是在下载该文件时,C# 中没有任何反应
- python-3.x - 仅存储“失败”芹菜任务的结果(芹菜结果后端)