node.js - MongoDB:如何通过舍入特定字段来查询文档
问题描述
假设 MongoDB 中有以下文档集合:
Scores
{ _id: 1, value: 3.25 }
{ _id: 2, value: 1.37 }
{ _id: 3, value: 2.48 }
{ _id: 4, value: 2.5 }
我想查询带有四舍五入value
的文档3
。所以它只会{ _id: 1, value: 3.25 }
返回{ _id: 4, value: 2.5 }
。
Node.js 查询类似于db.collection('scores').find({...})
我需要在查询中输入什么?
解决方案
我不确定是否可以直接使用任何查询运算符,
您可以通过数字的最小值和最大值边界在客户端准备条件,只需准备一个函数,例如,
function getValueCondition(n) {
return {
$gte: Math.round(n) - 0.5,
$lt: Math.round(n) + 0.5
};
}
你查询会得到条件,
let inputNumber = 3;
db.collection('scores').find({
value: getValueCondition(inputNumber)
});
推荐阅读
- python - 为什么我无法连接到 docker 容器中的烧瓶服务器?
- python - 我正在尝试使用 tkinter 创建一个带有两个组合框的简单 GUI。但是第一个组合框选择的值被第二个组合框覆盖
- android - Android - 每天在特定时间打开应用
- php - 如何在 Laravel 中处理多个 GET 请求
- java - 为什么休眠一段时间后会变慢?
- database-design - 适合电子商务的数据库架构
- django - 如何获取表单中的特定字段?
- regex - 如何删除括号内的文字和括号?
- mediawiki-api - MediaWiki:“attachedlocal”和“centralids”是什么意思?
- r - 带有 5 个参数的 Rcpp lapply() 或 mapply()