• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to use regular expressions

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}

 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
36
Scala Python Oracle Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?)
 
You didn't tell me he was so big. Unlike this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic