Mongodb mongoTemplate gets a separate field with some criteria

The structure of my MongoDB json

{ "_id" : "122134231234234", "name" : "Total_pop", "description" : "sales category", "source" : "public", "dataset" :"d1" }, { "_id" : "1123421231234234", "name" : "Total_pop", "description" : "sales category", "source" : "public", "dataset" :"d1" }, { "_id" : "12312342332423343", "name" : "Total_pop", "description" : "sales category", "source" : "private", "description" : "d1" } 

I need to assemble a collection other than a dataset where the source is publicly available. I tried this request and it did not work:

 Criteria criteria = new Criteria(); criteria.where("source").in("public"); query.addCriteria(criteria); query.fields().include("name"); query.fields().include("description"); query.fields().include("description"); query.fields().include("source"); List list = mongoTemplate.getCollection("collectionname").distinct("source", query); 

Could you help me?

+9
java mongodb spring-mongodb
source share
2 answers

On the one hand, the .getCollection() method returns the base object of the driver collection as follows:

 DBCollection collection = mongoTemplate.getCollection("collectionName"); 

So the type of request object may be different from what you are using, but there are other things. Namely, that .distinct() returns only the "distint" values ​​of the key that you requested, and do not return other fields in the document. So you can do:

 Criteria criteria = new Criteria(); criteria.where("dataset").is("d1"); Query query = new Query(); query.addCriteria(criteria); List list = mongoTemplate.getCollection("collectionName") .distinct("source",query.getQueryObject()); 

But this only returns the β€œpattern” as a separate item in the list, for example.

If you want the fields to be different from the set, use the .aggregate() method .aggregate() . Using the "first" events of other field values ​​for an individual key:

  DBCollection colllection = mongoTemplate.getCollection("collectionName"); List<DBObject> pipeline = Arrays.<DBObject>asList( new BasicDBObject("$match",new BasicDBObject("dataset","d1")), new BasicDBObject("$group", new BasicDBObject("_id","$source") .append("name",new BasicDBObject("$first","$name")) .append("description", new BasicDBObject("$first","$description")) ) ); AggregationOutput output = colllection.aggregate(pipeline); 

Or the actual "excellent" values ​​of several fields, making them part of the grouping key:

  DBCollection colllection = mongoTemplate.getCollection("collectionName"); List<DBObject> pipeline = Arrays.<DBObject>asList( new BasicDBObject("$match",new BasicDBObject("dataset","d1")), new BasicDBObject("$group", new BasicDBObject("_id", new BasicDBObject("source","$source") .append("name","$name") .append("description","$description") ) ) ); AggregationOutput output = colllection.aggregate(pipeline); 

There is also a direct .aggregate() method for .aggregate() instances, which has a number of helper methods for building pipelines. But that should point you in the right direction, at least.

+14
source share

As of Spring Data Mongo 2.2.0, MongoTemplate provides a function to retrieve a single field with criteria,

 Criteria criteria = new Criteria("country").is("IN"); Query query = new Query(); query.addCriteria(criteria); return mongoTemplate.findDistinct(query,"city",Address.class,String.class); 

Which basically finds all the individual cities in the address collection, where the country is IN.

0
source share

All Articles