15

My code need to support any query that being sent by the client . The client will be sending the query as a json . I have done this using java mongo driver low level api using following code ,
BasicDBObject queryObject = (BasicDBObject) JSON.parse(whereJson.toString());
As i am a newbie in spring data mongodb , i am unable to find a similar solution in either Query or Criteria classes . I have checked different tutorials and couldn't find any . Is it possible to do in spring data mongodb or should i use low level apis itself ?

Mohammed shebin
  • 479
  • 3
  • 11
  • 24

1 Answers1

20

You can create Query instances from a plain JSON String by using BasicQuery object. The following example demonstrates how you can construct a Query instance from a plain JSON String:

BasicQuery query = new BasicQuery("{ age : { $lt : 50 } }");
List<Person> result = mongoTemplate.find(query, Person.class);    

Another way which uses the low-level API:

DBObject dbObject = (DBObject) JSON.parse(query); 
DBCursor cursor = mongoTemplate.getCollection("person").find(dbObject); 

You can then map the return objects back to your Person POJO using the MongoConverter read() method:

List<Person> returnList = new ArrayList<Person>();
while (cursor.hasNext()) { 
    DBObject obj = cursor.next(); 
    Person person = mongoTemplate.getConverter().read(Person.class, obj);  
    returnList.add(person); 
} 
chridam
  • 100,957
  • 23
  • 236
  • 235
  • 2
    Wow. Thanks for this . I was stuck and waiting for a solution . Thanks a lot :) – Mohammed shebin Nov 02 '15 at 17:03
  • 2
    @Mohammedshebin No worries, happy to help :) – chridam Nov 02 '15 at 17:20
  • @chridam Hi, How to support aggregate query like db.CollectionName.aggregate( [ {$match:{"st":"i"}}, {$group:},{}.. ]) ...? – Kanagavelu Sugumar Jan 16 '17 at 15:29
  • @KanagaveluSugumar The Aggregation Framework support in Spring Data MongoDB is based on the key abstractions `Aggregation`, `AggregationOperation` and `AggregationResults` where `Aggregation` represents a MongoDB aggregate operation and holds the description of the aggregation pipeline instructions. Aggregations are created by invoking the appropriate `newAggregation(…)` static factory Method of the Aggregation class which takes the list of AggregateOperation as a parameter next to the optional input class. The aggregate operation is executed by the `aggregate()` method of the `MongoTemplate`. – chridam Jan 16 '17 at 15:33
  • Thanks! But the question is can i pass json aggregate query string directly to BasicQuery/JSON.parse() like above you have mentioned { age : { $lt : 50 } } ..? – Kanagavelu Sugumar Jan 16 '17 at 15:35
  • @KanagaveluSugumar So you should be able to do, for example `AggregationResults results = mongoTemplate.aggregate(agg, "INPUT_COLLECTION_NAME", OutputType.class); List mappedResult = results.getMappedResults();` If you create a question with the specific question context then I will be happy to answer it. – chridam Jan 16 '17 at 15:35
  • @KanagaveluSugumar The answer in this question may be able to address your needs http://stackoverflow.com/questions/39393672/mongodb-aggregate-push-in-java-spring-data – chridam Jan 16 '17 at 15:38