Spring MongoDB Query Criteria by keys of Map - spring

I have document
public class Person {
private String id;
private String email;
private String firstName;
private String lastName;
private Map<String, Boolean> hobbies;
}
And have request with Set of reqHobbies. I need to find persons from my collection with hobbies of set.
I find a solution like
query.addCriteria(Criteria.where("hobbies").all(reqHobbies)); - this work, if my hobbies is List/Set.
Thanks for the help!

Related

extract less number of columns from database table as defined in #Entity class and map to same entity pojo in spring boot

My #Entity class is
#Entity
class Demo{
#Id
private int id;
private int firstName;
private String lastName;
private String address;
}
And the #Repositiory Interface is having method as below
#Query(value="select d.id,d.firstName from demo d",nativeQuery=true)
List<Demo> fetchDetails();
Here exception is thrown as : The field "lastName" is not present in ResultSet
Do i need to create another pojo that contain id,firstName as variable and change fetchDetails() methods to as below:
#Query(value="select d.id,d.firstName from demo d",nativeQuery=true)
List<New Pojo class with only 2 fields that is to be selected> fetchDetails();
i want the partially selected resultset to get mapped to Entity Demo automatically.
I their any way to map these two columns to the Entity Demo
You can use Class-Based Projections that you can have a lot of constructor you need according to all fields you want to fetch
For example, here's a projection class for the Demo entity:
public class DemoDto {
private int id;
private int firstName;
private String lastName;
private String address;
// getters, equals and hashCode
}
public DemoDto(String firstName) {
this.firstName = firstName;
}
public DemoDto(int id, String firstName) {
this.id = id;
this.firstName = firstName;
}
public DemoDto(int id, String firstName, String address) {
this.id = id;
this.firstName = firstName;
this.address = address;
}
You must also define equals and hashCode implementations – they allow Spring Data to process projection objects in a collection.
In your repository you can add some query with JPQL Constructor like:
#Query(value="select new your.class.fullname.package.DemoDto(d.firstName) from Demo d")
List<DemoDto> fetchNameOnly();
#Query(value="select new your.class.fullname.package.DemoDto(d.id, d.firstName) from Demo d")
List<DemoDto> fetchIdAndNameOnly();
#Query(value="select new your.class.fullname.package.DemoDto(d.id, d.firstName, d.address) from Demo d")
List<DemoDto> fetchAllDetails();
Projections are introduced for that exact reason. Have a look at the documentation here
What you need is this, create an interface like this with the getter method for the fields you want in the result.
interface IdAndNameOnly {
String getFirstname();
int getId();
}
Modify the query like this. You do not need #Query for simple queries like the one you have.
List<IdAndNameOnly> findAll();
You can convert object of type IdAndNameOnly to your Entity type. But that doesn't make much sense. You can just get the fields which you need from the IdAndNameOnly object. If not what is the point of fetching fewer fields.
If I'm not mistaken you need to create a custom constructor and use it JPQL Constructor Expressions.
Something like this would do the job:
#Entity
class Demo{
#Id
private int id;
private int firstName;
private String lastName;
private String address;
public Demo() {
// JPA needs the default constructor
}
public Demo(int id, String firstName) {
this.id = id;
this.firstName = firstName;
}
}
And the usage something like this:
#Query(value="select new your.class.fullname.package.Demo(d.id,d.firstName) from Demo d")
List<Demo> fetchDetails();

Project embedded document fields after lookup operation

I want to do a join between Timesheet:
#Data
#AllArgsConstructor
#NoArgsConstructor
#Document(collection = TIMESHEET_COLLECTION)
public class Timesheet {
#Id
private ObjectId id;
private ObjectId employeeId;
private LocalDate date;
private String occupationTitle;
private BigDecimal salary;
private List<TimesheetEntry> entries;
}
and Employee (as embedded document):
#Data
#AllArgsConstructor
#NoArgsConstructor
#Document(collection = Employee.EMPLOYEE_COL)
public class Employee {
#Id
private ObjectId id;
private String registry;
private String cpf;
private String firstName;
private String lastName;
private String nickname;
private String phone;
private LocalDate dateOfBirth;
private LocalDate admissionDate;
private EmployeeOccupation occupation;
private EmployeePaymentPreferences paymentPreferences;
private Map<String, String> equipmentPreferences;
private Boolean active;
}
So I have this aggregation query, with match, lookup, unwind and projection operations.
Aggregation aggregation = Aggregation.newAggregation(matchTimesheetFilter(timesheetFilter), lookupEmployee(), unwindEmployee(), projectEmployee());
There are lookup and unwind implementations. I'm unwinding because employee should be a single object, not an array.
private LookupOperation lookupEmployee(){
return LookupOperation.newLookup()
.from("employee")
.localField("employeeId")
.foreignField("_id")
.as("employee");
}
private UnwindOperation unwindEmployee(){
return Aggregation.unwind("employee");
}
It returns successfully a Timesheet document with a embedded Employee document. The point is: I don't want all data from employee. I only want a few fields.
So, I tried to exclude unwanted fields from employee, using my projection operation:
private ProjectionOperation projectEmployee() {
return Aggregation.project().andExclude("employee.nickname", "employee.firstName", "employee.fullName");
}
It didn't work. My embedded employee is still being returned with all fields. However I can successfully exclude fields from Timesheet, if I do something like this:
private ProjectionOperation projectEmployee() {
return Aggregation.project().andExclude("startDate", "endDate");
}
How can I project custom fields from a document embedded through a lookup operation?
i think you need to exclude "employee.nickname", "employee.firstName", "employee.fullName", instead of "nickname", "firstName", "fullName"
Try this:
private ProjectionOperation projectEmployee() {
return Aggregation.project().andExclude("employee.nickname", "employee.firstName", "employee.fullName");
}
i did it this way (not sure if it's right but it works):
private LookupOperation lookupEmployee(){
return LookupOperation.newLookup()
.from("employee")
.localField("employeeId")
.foreignField("_id")
.as("employeeLookup");
}
no unwind used
Aggregation.project().and("employeeLookup.firstName").as("employee.firstName")

Spring data mongoDB: Get the distinct rows with pagination

I have User class like,
#Document(collection = "users")
public class User {
private String id;
private String firstName;
private String lastName;
private String jobTitle;
private String email;
private String phoneNumber;
}
And UserDimension as,
#Document(collection = "userDimensions")
public class UserDimension{
private String id;
private String userId;
private String dimensionId;
private String status;
}
I want the userDimension records from mongoDB based on distinct userId with pagination in spring data mongoDB ?
I am using the query like,
Query query = new Query();
List<UserDimension> userDimensionList = null;
// apply pagination parameters to the search criteria
query.with(pageable);
userDimensionList = mongoTemplate.getCollection("userDimensions").distinct("userId", query.getQueryObject());
But it still giving me the total records.

Groovy : fetching values of nested objects using xpath kind

I have POJO with nested objects which i need to translate to a simple object with out nesting
for example i have a Person and Address as below
public class Person {
private String firstName;
private String lastName;
private Address address;
}
public class Address {
private String lineOne;
private String lineTwo;
}
I need to translate Person to PersonFlat which looks like
public class PersonFlat {
private String firstName;
private String lastName;
private String Address_lineOne;
private String Address_lineTwo;
}
is there any way where i can do xpath kind of extraction on the Person instance to get the Address.lineOne and Address.lineTwo using groovy metaClass ?

How to search nested object by using Spring Data Solr?

I have two such Java object:
public class PSubject
{
#Column
#Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
#org.apache.solr.client.solrj.beans.Field("name")
private String name;
#Column
#Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
#org.apache.solr.client.solrj.beans.Field("type")
private String type;
#Column
#Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
#org.apache.solr.client.solrj.beans.Field("uri")
private String uri;
#OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
#IndexedEmbedded
#org.apache.solr.client.solrj.beans.Field("attributes")
private Set<PAttribute> attributes = new HashSet<PAttribute>();
.....
}
#Entity
#Indexed
#Table(name="PAttribute")
#Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class PAttribute extends PEntity
{
private static final long serialVersionUID = 1L;
#Column
#Field(index=Index.YES, analyze=Analyze.YES, store=Store.YES)
#org.apache.solr.client.solrj.beans.Field("attr_name")
private String name;
#Column
#Field(index=Index.YES, analyze=Analyze.YES, store=Store.YES)
#org.apache.solr.client.solrj.beans.Field("attr_value")
private String value;
.....
}
And my Spring Data Solr query interface:
public interface DerivedSubjectRepository extends SolrCrudRepository<PSubject, String> {
Page<PSubject> findByName(String name, Pageable page);
List<PSubject> findByNameStartingWith(String name);
Page<PSubject> findBy(Pageable page);
#Query("name:*?0* or description:*?0* or type:*?0* or mac_address:*?0* or uri:*?0* or attributes:*?0*")
Page<PSubject> find(String keyword,Pageable page);
#Query("name:*?0* or description:*?0* or type:*?0* or mac_address:*?0* or uri:*?0* or attributes:*?0*")
List<PSubject> find(String keyword);
}
I can search any by name, description, type and mac_address, but can't search any result by attribute.
Update:
For example,when user search "ipod", it's probably means the type of subject or name of subject, or the name of attribute or the value of attribute. And I want get all the matched subject in one request. I know I can search the attribute object in a separate query. But that makes the code in the backend complex.
So, how can I search this nested object?
Update:
I flattened my data:
#Transient
#Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
#org.apache.solr.client.solrj.beans.Field("attrs")
private String attrs;
public String getAttrs() {
return attrs;
}
public void setAttrs(Set<PAttribute> attributes) {
StringBuffer attrs = new StringBuffer();
if(attributes==null) {
attributes = this.getAttributes();
}
for(PAttribute attr:attributes){
attrs.append(attr.getName()+" " + attr.getValue()).append(" ");
}
this.attrs =attrs.toString();
}
The issue is resolved.
IIRC it is not possible to store nested data structures in solr - it depends how you flatten your data to fit into an eg. multivalue field - a little hard not knowing your schema.
see: http://lucene.472066.n3.nabble.com/Possible-to-have-Solr-documents-with-deeply-nested-data-structures-i-e-hashes-within-hashes-td4004285.html
How does the data look like in you index, and did you have a look at the http request sent by spring-data-solr?

Resources