• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use regular expressions

 
Mallory Liz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I have a method that returns a collection. The aim is to filter out data such that this collection has all ratings greater than 0 and less than 3 into it.

My expression is not working well. Is this the right format for using to select ratings greater than 0 and less than 3 as my collection is returning all the values. Any help with this will be highly appreciated.

public DBCollection displayRatingzerotothree() {
BasicDBObject query = new BasicDBObject();
collection = db.getCollection(Collections.COLLECTION.getName());
query.put("rating", new BasicDBObject("$gt", 0.0).append("$lte", 3.0));
DBCursor cursor = collection.find(query);
return collection;
}

 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see any need for regular expressions here. The MongoDB query syntax allows you to do this kind of query directly e.g. check out these examples, including this one (example 2.3 on the linked page):

Fetches documents (records) where the "number" field is > 2 and < 5.

If you're unsure of how to query stuff on MongoDB, maybe take some time to practice queries via the MongoDB shell, which provides a JSON API for queries. As an old RDBMS developer, I really like MongoDB's query tools, including the powerful and flexible aggregation framework. But just like with an RDBMS, it's usually better to build your query via the DB's shell (SQL for an RDBMS, or MongoDB shell here) first, to make sure the query's right before you mix it up with all the Java stuff.
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mallory Liz wrote:
public DBCollection displayRatingzerotothree() {
BasicDBObject query = new BasicDBObject();
collection = db.getCollection(Collections.COLLECTION.getName());
query.put("rating", new BasicDBObject("$gt", 0.0).append("$lte", 3.0));
DBCursor cursor = collection.find(query);
return collection;
}

Can you tell me where your function's return value comes from?

(Looks like we both hit an XY Problem when looking at this code, eh?)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic