Stream MongoDB Result of FindAll documents - java-8

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?

Related

How can I write a custom query with spring in mongo?

I'm trying to make a query and fetch data over mongodb with Spring. But I don't know mongo and spring very well. I have these fields in my table. How can I write a custom query with this query in SQL? I mean give the qp value of the record whose id is this. How can I write this please help..
SELECT qp
FROM tableName
WHERE id = request.getId();
my custom query method is it true?
public Object findQueryParams(ProcessInfo processInfo ){
Query query = Query.query(Criteria.where("id").is(processInfo .getId()).is(processInfo .getQueryParams()));
return query;
}

Spring MongoDB: How to remove strings from array by regex?

I have a collection named person with document like:
{
_id: 123456,
tags:["abc","abd","bcd"]
}
Now using Spring MongoTemplate I want to remove from tags where elements starts with ab, tried multiple ways but without luck.
For example, I tried but not working:
Update update = new Update().pull("tags",query(new Criteria().regex("^ab")));
Query query = query(where("_id").is(123456));
mongoTemplate.updateMulti(query, update, "person");

Adding Solr sort criteria to named query in Spring Data

I am trying to add a sort criteria to a Spring data Solr search.
I have seen methods for sorting search results (List<> .. after the data has been received from Solr) but not for specifying the sort order to Solr in the query.
The Solr query should look like....
http://localhost:8088/solr/books/select?fl=fullrecord%2C%20url&q=mod_date%3A%5B2012-04-17T11%3A38%3A15Z%20TO%20NOW%5D&sort=mod_date%20asc
I need to do this in Solr because the request is paged (there are potentially hundreds of thousands of results) and so only a limited number of results (one page) are returned for each query.
How can I add the "&sort=mod_date asc" solr query string?
// Catalog.findByDateChanged=mod_date:[?0 TO ?1]
public interface CatalogRepository extends SolrCrudRepository<CatalogDoc, String> {
#Query(name = "Catalog.findByDateChanged", fields = { "fullrecord", "url" })
public Page<CatalogDoc> findByDateChanged(String fromDate, String toDate, Pageable pageable);
}
It seem that the sort can be added to the call...
Page<CatalogDoc> results = solrRepo.findByDateChanged(from, to,
PageRequest.of(pageNo, perPage, Sort.Direction.ASC, "mod_date" ));
Curiously it adds it twice to the command to Solr...
&sort=mod_date+asc,mod_date+asc

How to Created Nested JSON objects in SOLR?

I am dumping data in SOLR database. Earlier I was using Elastic Search and it was allowing me to store nested JSON objects.
Is there any way I can dynamically create nested JSON values when inserting in SOLR?
I am using JAVA as a backend language. My code is:
SolrInputDocument document = new SolrInputDocument();
document.addField("UUID", eventID);
document.addField("eventCategory", eventCategory);
.
.
.
.
document.addField("source", source);
I want something like this:
{
"UUID":"1",
"source":abcd,
"eventCategory": {
"event1":"a",
"event2":"b",
"event3":"c"
}
}
Solr is a collection of flat schema documents. You can add dynamic field to solr but it does not support nested JSON object.
But you can use nested documents specified in following resource.
https://lucene.apache.org/solr/guide/6_6/uploading-data-with-index-handlers.html
But querying on child documents is not as straightforward as ElasticSearch or MongoDB.
you can use addChildDocument to add the nested doc to the parent one. So code would be:
SolrInputDocument document = new SolrInputDocument();
document.addField("UUID", eventID);
document.addField("eventCategory", eventCategory);
...
SolrInputDocument child = new SolrInputDocument();
...
document.addChildDocument(child);

Convert ObjectId to String in Spring Data

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\" }}");

Resources