I have following structure:
f1: {
f2: {
f3: "abc",
f4: "xyz"
}
}
I want to fetch documet using f3 AND f4 field. How can use I Spring data repository pattern for this kind of nested field query?
I was trying something like:
#query("{'f1.f2.f3': ?0, 'f1.f2.f4': ?1}")
List<MyObject> getData(String s1, STring s2);
This din't work.
Related
Is it possible for a native Hibernate/Spring database query to return an object structure that contains nested properties? Ultimately I want to return the data to a GraphQL query. I'm using Spring for GraphQL and want the following request satisfied:
query Query {
someField {
prop1
prop2
complexProp {
prop3
prop4
}
}
}
What does work is to return a flattened version of the graph as you see below. I'm okay with working with the flattened version, but I would prefer a graph with nested fields for complex objects. Here is what does work:
query Query {
someField {
prop1
prop2
prop3
prop4
}
}
I'm able to generate the above flattened graph by using Transformers.aliasToBean (aka, ResultTransformer), like so:
var query = """
select p.prop1 as prop1,
p.prop2 as prop2,
c.some_prop1 as prop3,
c.some_prop2 as prop4
from parent p
inner join child c on c.parent_id = p.id
""";
var results = (List<ParentWithChildFlattenedDto>) entityManager.createNativeQuery(query)
.setMaxResults(100)
.unwrap(NativeQuery.class)
.setResultTransformer(Transformers.aliasToBean(ParentWithChildFlattenedDto.class))
.getResultList();
If I were using managed entities instead of a native query, I know the nested graphql structure could be built by doing one of two things:
The "new" operator in JPQL
Interface projections
thx
I'm using interface based projection to get certain fields from db. one of my field name start with is. I'm able to get the field from database via native query, however, the response returned by spring boot controller does not contain is in field name. How should I resolve it?
interface UserProjection {
val userId: Long
val isPrivate: Boolean
val likesCount: Int
}
Query
SELECT u.user_id as userId, u.is_private as private, u.likes_count as likesCount FROM users u WHERE u.user_id=?;
However, response returned by spring boot is
{
"userId": 12345,
"private": false,
"likesCount": 1
}
The solution is to use a fun to get the field instead of a val
interface LoginUserProjection {
val id: Long
val passwordHash: String
fun getIsVerifiedAccount(): Boolean
}
Is it possible to have field type that would look like List>?
So I have Java POJOs:
public class Document {
//omitted other fields
private List<ChildDocument> values;
class ChildDocument {
private List<String> words;
private Timerange timerange;
}
class Timerange {
// time in format "10:15:30.222"
// LocalTime from Java-8 time api
private LocalTime start;
private LocalTime end;
}
}
Example data in such field:
index -> {time-range -> list of strings}
(0) -> "08:00:00.000 - 09:00:00.000" -> {"1", "2", "3"}
(1) -> "11:00:00.000 - 13:00:00.000" -> {"4", "5", "6"}
And I would query "find 'time-range' that has '5' in it" and I would get "08:00:00.000 - 09:00:00.00" as property defined as in my Java ChildDocument. Is it possible to store in ES just time-range without dates? And do this kind of nested objects? I know I cound store that TimeRange field as String and parse it back in my Java app but trying to optimize it so ES does most of the work.
Still new to ES (came from Solr and that's impossible there).
I have a document like that:
'subject' : {
'name' :"...."
'facebookPosts':[
{
date:"14/02/2017 20:20:03" , // it is a string
text:"facebook post text here",
other stuff here
}
]
}
and I want to count the facebookPosts within a specific objects that their date field contains e.g "23/07/2016".
Now, I do that by extracting all the documents and count in the client side (spring ) , But I think that's not efficient.
You need to aggregate your results.
final Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(Criteria.where("facebookPosts.date").regex(REGEX)),
Aggregation.unwind("facebookPosts"),
Aggregation.group().count().as("count"));
Regex might not be the best solution, just an example.
unwind will split array into separate elements you can then count.
Create a class that will hold the count, something like:
public class PostCount {
private Long count;
// getters, setters
}
And then execute it like this:
AggregationResults<PostCount> postCount = mongoTemplate.aggregate(aggregation, Subject.class, PostCount.class);
long count = postCount.getMappedResults().get(0).getCount();
I am having trouble to query that, I want to find all the docs that contains the id "5418a26ce4b0e4a40ea1d548" in the individualUsers field. would be esp useful if you know how to do that in Spring Data MongoDB query.
db.collection.find({individualUser:{"5418a26ce4b0e4a40ea1d548"}})
example of one doc
{ "_id" : ObjectId("5418c3b9e4b03feec4345602"), "creatorId" : "5418a214e4b0e4a40ea1d546", "individualUsers" : { "5418a26ce4b0e4a40ea1d548" : null, "5418a278e4b0e4a40ea1d54a" : null } }
Update #001
Entity code
#Document
class Idea{
#Id
String id;
String creatorId;
Map<String,String> individualUsers;
/*getter and setter omitted*/
}
Interface
public interface IdeaRepository extends MongoRepository<Idea,String> {
}
Update #002
So when spring-mongodb saves hashmap to json, it will look like
"individualUsers" : { "5418a26ce4b0e4a40ea1d548" : null, "5418a278e4b0e4a40ea1d54a" : null }
In java program I can easily get the data using the key value. but in the mongodb query, I can't query the key?
so the question is can I query inside the "individualUsers": {} with the key ??