mongodb中的查询语法为“之间” - 我正在使用java

时间:2012-05-19 12:56:59

标签: java java-ee mongodb

我想检索其“价格”字段介于某个最大值和最小值之间的数据,我在java中使用下面的语法,但似乎它无效。

>

 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());

}
你能看出我做错了吗?

- 编辑

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

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

1 个答案:

答案 0 :(得分:2)

您希望将价格与数字进行比较,而不是字符串。所以你想得到一个看似如下的条件对象:

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