Mitul
Mitul

Reputation: 3437

Check the value input between two column value.

I have records which contain 3 columns Title MinValue MaxValue.

Now i am work on filter and wan to get the title of the my inputted value.

Records :

[
    {
        title : "1-10",
        minValue : 1, 
        maxValue : 10
    },
    {
        title : "11-20",
        minValue : 11, 
        maxValue : 20
    },
    {
        title : "21-30",
        minValue : 21, 
        maxValue : 30
    }
]

I need mondodb query which will return the record

consider I input 5 then it will return first record

Upvotes: 0

Views: 12

Answers (2)

Buzz Moschetti
Buzz Moschetti

Reputation: 7588

Here's a solution:

var target = 5;

db.foo.find({minValue: {"$lt":target}, maxValue: {"$gt":target}}, {_id:0,title:1});

The second arg to findis the projection. It says do NOT show _id but do show title.

Upvotes: 1

hurricane
hurricane

Reputation: 6734

You can use $gt and $lt parameters.

.find({ minValue : { $gt :  5}, maxValue : { $lt : 5 }})

Upvotes: 1

Related Questions