我想检索其“价格”字段在某个最大值和最小值之间的数据,我在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"}}]}
最佳答案
您想将价格比较为数字,而不是字符串。因此,您最终需要一个条件对象,如下所示:
{ "Price": {"$gt": 2, "$lt": 1250 } }
关于java - 查询mongodb中“之间”的语法-我正在使用Java,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10665388/