Convert ObjectId to String in Spring Data - spring

How can I reference two mongodb collections using spring data while the localField is of type ObjectId and foreignField is of type String?
ProjectionOperation convertId=Aggregation.project().and("_id").as("agentId");
LookupOperation activityOperation = LookupOperation.newLookup().
from("activity").
localField("agentId").
foreignField("agent_id").
as("activities");
Aggregation aggregation = Aggregation.newAggregation(convertId,activityOperation);
return mongoTemplate.aggregate(aggregation, "agents", AgentDTO.class).getMappedResults()
However, this doesn't return any records because of the type issue. Is it possible to implement $toString or $convert in ProjectionOperation? or what other options are there?

I was able to solve it by writing native mongodb aggregation operation in java code as described in MongoDB $aggregate $push multiple fields in Java Spring Data
After implementing this solution I was able to add native $addfields as follows:
AggregationOperation addField=new GenericAggregationOperation("$addFields","{ \"agId\": { \"$toString\": \"$_id\" }}");

Related

Stream MongoDB Result of FindAll documents

I want to write a mongo query to fetch All Documents in a collection and project just 2 fields and no criteria.
I want to use streams here and iterate over the list of documents to process them.
How can I write this in Java Spring Mongo?
I have something like this:
public CloseableIterator<Employees> streamAllEmployees() {
Query query = new Query();
query.fields().include("_id.name","_id.dept");
query.addCriteria(Criteria.where("_id.dept").is(1));
return mongoOperations.stream(query, Employee.class);
}
but how can I change this to not have any QueryCriteria?

Retrieve aggregations using Spring Data Elasticsearch Reactive Template

We are using Spring Data Elasticsearch Reactive Template
Query searchQuery = new NativeSearchQueryBuilder()
.withQuery(queryBuilder)
.withPageable(PageRequest.of(0, 10))
.addAggregation(AggregationBuilders.terms("categories").field("category"))
.build();
reactiveElasticsearchTemplate.search(searchQuery, documentType, IndexCoordinates.of(indexName))
In response we have Flux<SearchHit<T>> but there are no methods to retrieve aggregations.
How to retrieve the aggregations?
The ReactiveElasticsearchTemplate has aggregate methods.
See the corresponding API interface
There is no combination of the single entities in a flux and the aggregations in the reactive part.

How to set OpType on IndexQuery in Spring Data Elasticsearch

Assume spring-boot-starter-data-elasticsearch version 2.1.0.RC1.
Take the following, simple implementation for indexing an entity:
IndexQuery indexQuery = new IndexQueryBuilder().withId(entity.getId()).withObject(entity).build();
String id = elasticsearchTemplate.index(indexQuery);
How do I set the OpType.CREATE on this operation, so that I can assure only documents get indexed which don't already exist?
The equivalent REST API request would look like the following:
POST /{index}/{entity id}?op_type=create
{
"id" : "{entity id}",
"attribute" : "value"
}
This is not supported at the moment by Spring Data ES.
There's a open issue that reports exactly that feature, you might want to check it out: https://jira.spring.io/browse/DATAES-247

How to make a custom sorting query in spring boot for a mongo db repository?

I want to put this query with #Query annotation in my repository.
This is the query:
`db.report.find({'company' : 'Random'}).sort( { 'reportDate' : -1} ).limit(1)`
Which is the best way to implement custom queries with #Query annotations or to use MongoTemplate ?
Using Mongo Template.
Criteria find = Criteria.where("company").is("Random");
Query query = new Query().addCriteria(find).with(new Sort(Sort.Direction.DESC, "reportDate"));
BasicDBObject result = mongoOperations.findOne(query, BasicDBObject.class, "collection_name");
Using Mongo Repository
Report findTopByCompanyOrderByReportDateDesc(String company)
Note that in the new version of Springboot(v2.2.5), the correct method is sort.by().
like this:
Query query = new Query().addCriteria(find).with(Sort.by(Sort.Direction.DESC, "reportDate"));

Spring data jpa Query dynamically pass where clause

My Entity is in this way
public class event
{
String title;
String description;
String city;
}
I am new to Spring data jpa ,i want implement search feature when an user enters "Hello Hyderabad Fest"
I want token size the string and split into words and find Any word matches on any properties on entity with search query hit to db.
WHERE title LIKE '%Hello%' OR title LIKE '%Hyderabad%' OR title LIKE
'%Fest%' OR description LIKE '%Hello%' OR description LIKE
'%Hyderabad%' OR description LIKE '%Fest%'city LIKE '%Hello%' OR
cityitle LIKE '%Hyderabad%' OR city LIKE '%Fest%'
How can we achieve this in spring data jpa.
Can we dynamically pass where condition in Spring data jpa named queries
Can we lucene kind query which we use in nosql dbs.
any other suggestion
Thanks in advance.
Postgresql fulltext search query solved the above issue http://rachbelaid.com/postgres-full-text-search-is-good-enough/

Resources