MoienGK
MoienGK

Reputation: 4654

Query Syntax in mongodb for "between" - i am using java

i want to retrieve data which their "price" field is between a certain max and min , i am using syntax below in java but it seems that it is not working .

>

 BasicDBObject numeralQ;                                                                
List<DBObject> clauses = new ArrayList<DBObject>();
.
.
.
.
numeralQ.put(datafield,new BasicDBObject("$gt", min).append("$lt", max));
clauses.add(numeralQ);  
.
.//i also have many other conditions that i want to "OR" them with this condition
.
BasicDBList or = new BasicDBList();

for (DBObject clause : clauses)
    or.add(clause);

DBObject query = new BasicDBObject("$or", or);
DBCursor cur = coll.find(query);

while (cur.hasNext()) {
    System.out.println(cur.next());

}

can you see what am i doing wrong?

--EDIT

printing the `query.toString();` results in this string :

{ "$or" : [ { "Price" : { "$gt" : "2" , "$lt" : "1250"}}]}

Upvotes: 0

Views: 3372

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 312035

You want to compare the price as numbers, not strings. So you want to end up with a condition object that looks like:

{ "Price": {"$gt": 2, "$lt": 1250 } }

Upvotes: 4

Related Questions