mongo db querying array values with greater and less than

arrays, mongodb

Solution

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 ] }

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]}})

But not pretty...

Problem

I have a schema similar to ``` { 'user_id' : 1, 'marks' : [10, 40] } ``` I want to find the number of users who have scored marks between 20 and 30 at least once. I tried the following query ``` db.users.count({ 'marks' : {$gte: '20', $lt: '30'} }) ``` But this also included those with marks more than 30 ...

Original source

Related problems