Let's suppose there is the following collection of documents in MongoDB:
Scores
{ _id: 1, value: 3.25 }
{ _id: 2, value: 1.37 }
{ _id: 3, value: 2.48 }
{ _id: 4, value: 2.5 }
I would like to query documents with the rounded value
of 3
. So it will return { _id: 1, value: 3.25 }
and { _id: 4, value: 2.5 }
only.
The Node.js query would be something like db.collection('scores').find({...})
What do I need to put in the query?
I am not sure it possible to directly using any query operators,
you can prepare condition on the client-side by number's minimum and maximum value boundry, just prepare a function like,
function getValueCondition(n) {
return {
$gte: Math.round(n) - 0.5,
$lt: Math.round(n) + 0.5
};
}
You query would be and get condition,
let inputNumber = 3;
db.collection('scores').find({
value: getValueCondition(inputNumber)
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.