0

我想检索其“价格”字段介于某个最大值和最小值之间的数据,我在 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"}}]}

4

1 回答 1

4

您想将价格作为数字而不是字符串进行比较。所以你想得到一个看起来像这样的条件对象:

{ "Price": {"$gt": 2, "$lt": 1250 } }
于 2012-05-19T13:48:14.563 回答