本文介绍了mongo db查询大于和小于的数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类似于
{
'user_id' : 1,
'marks' : [10, 40]
}
我想找到至少一次得分在20到30之间的用户数量.
I want to find the number of users who have scored marks between 20 and 30 at least once.
我尝试了以下查询
db.users.count({
'marks' : {$gte: '20', $lt: '30'}
})
但这还包括那些标记超过30个的标记...
But this also included those with marks more than 30 ...
推荐答案
因此,假设您在收集分数中具有以下数据:
So assuming you had the following data in collection scores:
{ 'user_id' : 1, 'marks' : [ 10, 40 ] }
{ 'user_id' : 2, 'marks' : [ 5, 18 ] }
{ 'user_id' : 3, 'marks' : [ 15, 25 ] }
{ 'user_id' : 4, 'marks' : [ 22, 33 ] }
然后,您希望过滤user_id 3和4,计数为2.
Then you would expect to filter user_ids 3 and 4, a count of 2.
运行查询的最简单方法是:
The simplest way to run your query is to do:
db.scores.count({'marks':{$in: [20,21,22,23,24,25,26,27,28,29,30]}})
但是不漂亮...
这篇关于mongo db查询大于和小于的数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!