Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java MongoDB Query Criteria (WHERE date > X and field = value) Ignores Second Clause

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

public class CustomQuery {

  @Autowired private MongoOperations mongoOperations;    

  public void customQuery(Date submittalDate) {

     List<Question> q1s = mongoOperations.find(
        new Query(Criteria.where("category").is("New")), 
        Question.class);

     List<Question> q2s = mongoOperations.find(
      new Query(
        Criteria.where("submittalDate").gt(submittalDate).and("category").is("New")
      ),
      Question.class);
  }
}

The top Spring Java MongoDB query gives back the expected results in q1s.

The bottom query should return a subset of the top query. Instead, records which match ("submittalDate").gt(submittalDate) are in the q2s results regardless of whether or not they are in the "New" category.

i.e. it is like and("category").is("New") from the second query is being ignored.

Using Mongodb version v2.0.6 32-bit with Spring Data.

Help appreciated.

Update 05/09/2012

Still doesn't work

Update 26/08/2012

This returns results on the Mongo command line:

db.foo.find( {   "submittalDate":{ "$gte": ISODate("2012-07-31T23:00:00.000Z")  }, "category" : "New"    } )

In constrast, the Java code (for the same date paramter) doesn't work. For comparison, the query logged by DEBUG from Java is:

[DEBUG] [http-8080-1] (MongoTemplate.java:doFind:1256) find using query: 
{ "submittalDate" : { "$gte" : { "$date" : "2012-07-31T23:00:00.000Z"}} , "category" : "New"} 

Yes, the logging logs a date string whereas to get Mongo shell working I needed to use ISODate(..). But I'm using MongoDB Java driver with the accepted type of java.util.Date - how could ISODate(..) not appearing be the issue? Issue might have another cause.

like image 826
Smashing Turnip Avatar asked Nov 13 '22 00:11

Smashing Turnip


1 Answers

I'm no spring expert, but it seems like some of your imports may be conflicting with each other. It's difficult to diagnose exactly where you are going wrong given the documentation I've looked at. If your not set on using the spring framework for this, an alternative/more common approach would be the below.

import com.mongodb.Mongo;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;

public class CustomQuery {

public void customQuery(Date submittalDate)
{
        document = new BasicDBObject();
        document.put(("submittalDate").greaterThanEquals(submittalDate).put("category").is("New").get());
        DBCursor cursor = getDbCollection().find(document); 

}

}
like image 101
ChadsworthIII Avatar answered Jan 15 '23 20:01

ChadsworthIII