如何使用Mongoose从Mongodb获取大于等于整数值?假设下面的列表
list = [134,56,89,89,90,200] //Marks field想要得到大于或等于89的值,结果集必须是[89,90,200]在我的查询中我能够得到大于89的值,我想用89得到它
let x = 89;
query.find().where('marks').gt(x);发布于 2018-04-26 20:35:49
我不知道您使用的是什么版本的mongodb。但这是最新版本中提供的内容。
$gte选择字段值大于或等于(即>=)指定值的单据
query.find( { marks: { $gte: 89} } )发布于 2020-04-26 01:23:08
只需使用
.gte(upperlimit)对于大于等于and
.lte(lowerlimit)小于等于。
记住上限和下限应该是数字。
发布于 2020-04-24 04:26:37
Query.prototype.gt方法与使用“大于”运算符相同。
在您的示例中,您必须使用gte方法返回您所需要的内容-大于或等于给定值的结果:
let x = 89;
query.find()
.where('marks')
.gte(x)
.exec();此外,正如另一个答案所提到的,您始终可以使用原生MongoDB操作符$gte,但是Mongoose通过其链式查询API提供了更直观的方法,如上所述。
此外,如果你正在使用Mongoose with Promises或async/await,别忘了在最后添加exec() -这会将兼容thenable的查询结果转换为功能齐全的Promise,例如:
let x = 89;
find().where('marks').gte(x).exec()
.then(() => {/* rest of handling*/})
.catch(() => { /* error handling */});使用async/await的相同代码
async function getResultsGreaterThan(x) {
const results = await find().where('marks').gte(x).exec();
console.log(results);
return results;
}
getResultsGreaterThan(89);关于Mongoose chainable Query API的更多信息可以在相当可靠的官方文档https://mongoosejs.com/docs/api/query.html中找到
https://stackoverflow.com/questions/50043003
复制相似问题