我想检索其“价格”字段介于某个最大值和最小值之间的数据,我在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"}}]}
答案 0 :(得分:2)
您希望将价格与数字进行比较,而不是字符串。所以你想得到一个看似如下的条件对象:
{ "Price": {"$gt": 2, "$lt": 1250 } }