E.g. I have Java objects:
public class Foo {
private Example example;
}
public class Example {
private String str1;
private String str2;
}
Field example can be null.
I need to get all Foo objects where str1 contains e.g. "text". According to documentation I tried:
#Query(value = "{ 'example' : { $exists : true, $elemMatch : { str1 : { $regex: '.*?0.*'} } } }")
but it returns empty Page.
Define the query in the repository:
#Repository
public interface FooRepo extends MongoRepository<Foo, String> {
#Query("{ 'example' : { $exists : true }, 'example.str1' : { $regex: ?0 } }")
List<Foo> findByExamplePropertyRegex(String regexStr);
}
Sample four documents in foo collection:
{ "example" : { "str1" : "apple", "str2" : "rose" } },
{ "example" : { "str1" : "pineapple", "str2" : "jasmine" } },
{ "other": "stuff" },
{ "example" : null }
Run the query from Spring Boot application using CommandLineRunner:
#Autowired
private FooRepo repo;
// ...
public void run(String... strings) throws Exception {
String regexStr = "apple"; // -or- "in"
List<Foo> list = repo.findByExamplePropertyRegex(regexStr);
list.forEach(System.out::println);
The output will be two documents with the regexStr is "apple", and one document with input "in".
Also, see: $regex operator.
Related
I am looking for a solution that have Jackson use toString method whenever it can not serialize an object type.
Let me explain more detail.
I have a class:
#AllArgsConstructor
public class TestJackson {
public String RequestId;
public AntiSerialize foo;
#JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
public LocalDateTime dateRequest;
public Map<String, Object> headers;
private static class AntiSerialize {
#Override
public String toString() {
return "AntiSerialize " + ZonedDateTime.now().toEpochSecond();
}
}
public static TestJackson createSample() {
return new TestJackson(
"123",
new TestJackson.AntiSerialize(),
LocalDateTime.now(),
Map.of("content", 999,
"b3", new TestJackson.AntiSerialize(),
"b4", Arrays.asList(
new TestJackson.AntiSerialize(),
new TestJackson.AntiSerialize()
)
)
);
}
}
This is to test serialize object
var OM = new ObjectMapper()
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
try {
//var f = TestJackson.createSample().foo;
//out.println(((Object)f).toString());
var json = OM.writerWithDefaultPrettyPrinter()
.writeValueAsString(TestJackson.createSample());
out.println(json);
} catch (Exception e) {
e.printStackTrace(out);
}
It prints
{
"RequestId" : "123",
"foo" : { },
"dateRequest" : "2022-08-04 09:04:14",
"headers" : {
"b3" : { },
"b4" : [ { }, { } ],
"content" : 999
}
}
But I expect:
{
"RequestId" : "123",
"foo" : "AntiSerialize 1659578741",
"dateRequest" : "2022-08-04 09:04:14",
"headers" : {
"b3" : "AntiSerialize 1659578752",
"b4" : [ "AntiSerialize 1659578763", "AntiSerialize 1659578774" ],
"content" : 999
}
}
Assume TestJackson is third party class and can't modify.
AntiSerialize is also a private nested class.
So My problem is related to how spring handles inheritance when it comes to fields.
I have four classes
#Document(collection = "A")
public class A {
#Id
id;
#Field
B data;
}
public class B {
Type type
}
public class C extends B {
String cField;
public C() {
super(Type.C);
}
}
public class D extends B {
Integer dField;
public D() {
super(Type.D);
}
}
I'm able to store data into mongodb using this schema and it looks something like this
{
"_id" : ObjectId("5f291f861020d19a3db05123"),
"data" : {
"cField" : "abcd",
"type" : "C",
"_class" : "com.example.C"
},
"_class" : "com.example.A"
}
{
"_id" : ObjectId("5f291f861020d19a3db05124"),
"data" : {
"dField" : NumberInt(30),
"type" : "D",
"_class" : "com.example.D"
},
"_class" : "com.example.A"
}
Now when I try to do a findAll on repository, it does not casts to the child class, but simply returns an instance of B.
What changes do I need to make so that I can get correct child object when getting from db?
I have a Java list representation like below
List representation of data
[
{ "type" : "Error", "name" : "xyz" },
{ "type" : "Success", "name" : "abc" },
{ "type" : "none", "name" : "prq" },
{ "type" : "Success", "name" : "" },
{ "type" : "Success", "name" : "xyz" },
{ "type" : "Warning", "name" : "efc" }
.
.
.
]
(Partial representation here).
and an Object representation below
public Node {
List<String> errorNames;
List<String> SuccessNames;
List<String> WarningNames;
}
I want to use Java streams to separate the three type of names based on their type and add each name to the respective Lists.
What will be the best way (Stream.filter/Collect/Map anything else) to split the list such that at the end "Node's->(all the lists)" will have corresponding data?
Assuming your Node class actually looks something like this:
public class Node {
private String type;
private String name;
public Node(String type, String name) {
this.type = type;
this.name = name;
}
public String getType() {
return type;
}
public String getName() {
return name;
}
}
You can use Collectors#groupingBy in combination with Collectors#mapping to create a Map<String, List<String>> where they key is the type and the value are the List of name for every Node of that type:
var nodes = List.of(
new Node("Error", "xyz"), new Node("Success", "abc"),
new Node("none", "prq"), new Node("Success", ""),
new Node("Success", "xyz"), new Node("Warning", "efc")
);
var map = nodes.stream()
.collect(Collectors.groupingBy(Node::getType,
Collectors.mapping(Node::getName, Collectors.toList())));
System.out.println(map);
Output:
{Warning=[efc], Error=[xyz], none=[prq], Success=[abc, , xyz]}
My repository implements the following interface:
QueryDslPredicateExecutor<Rule>
The structure of mongo's document(Rule object) is:
{
"_id" : ObjectId("5aa924242a73bec4ce581717"),
"name" : "test-name",
"expressionGroups" : [
{
"type" : "WHEN",
"expressions" : [
{
"name" : "name1",
"values" : ["VAL1", "VAL2", "VAL3"]
},
{
"name" : "name2",
"values" : ["VAL4", "VAL5"]
}
]
},
{
"type" : "FOR",
"expressions" : [
{
"name" : "name3",
"values" : ["VAL6", "VAL7"]
}
]
}
]
}
I want to use the following method to find particular rule within mongodb:
Iterable<T> findAll(Predicate predicate);
I've prepared mongo shell query:
db.rule.find({
'expressionGroups.expressions': {
$all: [
{
'$elemMatch': {
'name': "name1",
'values': "VAL2"
}
},
{
'$elemMatch': {
'name': "name3",
'values': "VAL7"
}
}
]
}
}).pretty()
How can I create com.querydsl.core.types.Predicate based on above query?
There is still no answer and I've met the same issue. So I created such code:
private List<Predicate> toPredicates(String root, Map<String, Object> map) {
List<Predicate> predicates = new ArrayList<>();
for (Map.Entry<String, Object> entry: map.entrySet()) {
String path = root+'.'+entry.getKey();
Object value = entry.getValue();
if (entry.getValue() instanceof Map) {
predicates.addAll(toPredicates(path, (Map<String, Object>) value));
} else {
predicates.add(new SimplePath(path).eq(value.toString()));
}
}
return predicates;
}
private static class SimplePath extends StringPath {
protected SimplePath(String var) {
super(var);
}
}
So, you can parse Json to Map and this is it.
I'm having trouble getting my objects to sort.
Some initial knowledge:
I'm using MongoDB to store my collection, I'm able to retrieve them and get everything back correctly.
I have a class that implements Comparable, with a compareTo function, but I also want to be able to sort on different properties, thus I've added static comparables for each property I want to sort on.
public class PlaceHolder implements Comparable<PlaceHolder>{
private String name;
private String icon;
private String originalLangcode;
//Getters and setters + constructors here, these work 100%.
#Override
public int compareTo(PlaceHolder ph) {
return this.getName().compareTo(ph.getName());
}
public static Comparator<PlaceHolder> nameComparator = new Comparator<PlaceHolder>() {
#Override
public int compare(PlaceHolder ph1, PlaceHolder ph2) {
return ph1.getName().compareTo(ph2.getName());
}
};
public static Comparator<PlaceHolder> iconComparator = new Comparator<PlaceHolder>() {
#Override
public int compare(PlaceHolder ph1, PlaceHolder ph2) {
return ph1.getIcon().compareTo(ph2.getIcon());
}
};
public static Comparator<PlaceHolder> nativeLangComparator = new Comparator<PlaceHolder>() {
#Override
public int compare(PlaceHolder ph1, PlaceHolder ph2) {
return ph1.getNativeLang().compareTo(ph2.getNativeLang());
}
};
}
I've then wrote a function that gets all placeholders from my mongodb, returning them in a list with PlaceHolder objects.
public List<PlaceHolder> getAllPlaceholders(String sortType) {
List<PlaceHolder> list = getPlaceholderList();
switch(sortType) {
case "name":
Collections.sort(list, PlaceHolder.nameComparator);
break;
case "icon":
Collections.sort(list, PlaceHolder.iconComparator);
break;
case "native":
Collections.sort(list, PlaceHolder.nativeLangComparator);
break;
default:
Collections.sort(list, PlaceHolder.nameComparator);
break;
}
return list;
}
I always get my data unsorted:
{ "_id" : { "$oid" : "56653f82972552a4024814a3"} , "name" : "testHolder" , "icon" : "archive" , "originalLangcode" : "ENG"}
{ "_id" : { "$oid" : "5665427a97253f798067c57b"} , "name" : "doItHolder" , "icon" : "car" , "originalLangcode" : "ENG"}
{ "_id" : { "$oid" : "566545dd9725050a53b4a5a8"} , "name" : "testableHolder" , "icon" : "adjust" , "originalLangcode" : "ENG"}
{ "_id" : { "$oid" : "5665479b972511264f55aae1"} , "name" : "dataHolder" , "icon" : "hdd-o" , "originalLangcode" : "ENG"}
I'm failing to see what goes wrong.
I've debugged the comparables, and they seem to work, returning negatives and positives. But the list just doesnt seem to get sorted.
I'm using getAllPlaceholders in my controller, passing it to my Page handler which in turn generates html for a table view.
public class PlaceHolderControllerF extends ControllerAbF {
#Autowired PlaceHolderRepo pr;
#RequestMapping(value = "placeholderlist", method = RequestMethod.GET)
#ResponseBody
public String editLanguage(HttpSession httpSession) {
if (getUser(httpSession) == null) {
return noPermission();
}
return Pages.getPlaceHolderList(pr.getAllPlaceholders("name"));
}
Pages just gets my index html, passes some variables to it that then runs through my templater which fills in the variables into the html.