File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes MongoDB and the fly likes How to use regular expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » MongoDB
Bookmark "How to use regular expressions" Watch "How to use regular expressions" New topic
Author

How to use regular expressions

Mallory Liz
Greenhorn

Joined: Apr 15, 2013
Posts: 3
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

Joined: Mar 01, 2009
Posts: 1845
    
  16

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.


No more Blub for me, thank you, Vicar.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1845
    
  16

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?)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use regular expressions