Basically I have a simple POJO class which has some variables. Whenever I try using .save() it saves all values fine except for List<> values. Im storing to couchbase and the end result is something like this:
"location": "location",
"categories": {
"empty": false
}
Categories is a List and what I pass into my Postman body is:
"categories": [
"OneTwoThree"
]
but as you can see the serialization isnt done correct and I always get the "empty": false even if its my own class or a String. Any ideas what might be causing this?
This is https://jira.spring.io/browse/DATACOUCH-560. It is fixed in the working 4.1.0 snapshot and will be in the upcoming 4.0.2 release scheduled for tomorrow.
Lets say i have a sample Json Request Body stored in an Oracle DB which i'll be using it multiple times in Java code(Doing POST method using RestAssured)
Sample json Req. Ex:
xxxRequest{
yyyDetails{
"accountId": "????",
"Action": "Add",
"Address": "Brandon valley",
"PinCode": "12356",
}
}
Scenario: Saying i've 50 different TestCases where i'll be using this same Json Request. But only the value of "accountId" alone changes for all 50 TCs.
Once i connect with DB, storing this Json request in a String, I need to change the "accountId" key's value alone each time before i do POST method.
Note: I tried splitting the String into two and add my required AccountID value everytime.
Wanted to know if there's any other better way to make it simple.
Have your accountid value hardcoded to something like "ACCOUNTID" and replace the value each time you send a specific request
I have some mongo data that looks like this
{
"_id": {
"$oid": "5984cfb276c912dd03c1b052"
},
"idkey": "123",
"objects": [{
"key1": "481334",
"key2": {
"key3":"val3",
"key4": "val4"
}
}]
}
I want to know what the value of key4 is. I also need to filter the results byidkey and key1. So I tried
doc = mongoCollection.find(and(eq("idKey", 123),eq("objects.key1", 481334))).first();
and this works. But i want to check the value of key4 without having to unwrap the entire object. Is there some query i can perform that gives me just the value of key4? Note that I can update the value of key4 as
mongoCollection.updateOne(and(eq("idKey", 123), eq("objects.key1", 481334)),Updates.set("objects.$.key2.key4", "someVal"));
Is there a similar query i can run just to get the value of key4?
Upadte
thanks a lot #dnickless for your help. I tried both of your suggestions but i am getting null. Here is what i tried
existingDoc = mongoCollection.find(and(eq("idkey", 123), eq("objects.key1", 481334))).first();
this gives me
Document{{_id=598b13ca324fb0717c509e2d, idkey="2323", objects=[Document{{key1="481334", key2=Document{{key3=val3, key4=val4}}}}]}}
so far so good. next i tried
mongoCollection.updateOne(and(eq("idkey", "123"), eq("objects.key1", "481334")),Updates.set("objects.$.key2.key4", "newVal"));
now i tried to get the updated document as
updatedDoc = mongoCollection.find(and(eq("idkey", "123"),eq("objects.key1","481334"))).projection(Projections.fields(Projections.excludeId(), Projections.include("key4", "$objects.key2.key4"))).first();
for this i got
Document{{}}
and finally i tried
updatedDoc = mongoCollection.aggregate(Arrays.asList(Aggregates.match(and(eq("idkey", "123"), eq("objects.key1", "481334"))),
Aggregates.unwind("$objects"), Aggregates.project(Projections.fields(Projections.excludeId(), Projections.computed("key4", "$objects.key2.key4")))))
.first();
and for this i got
Document{{key4="newVal"}}
so i'm happy :) but can you think of a reason why the firs approach did not work?
Final answer
thanks for the update #dnickless
document = collection.find(and(eq("idkey", "123"), eq("objects.key1", "481334"))).projection(fields(excludeId(), include("key4", "objects.key2.key4"))).first();
Your data sample contains a lowercase "idkey" whereas your query uses "idKey". In my examples below, I use the lowercase version. Also you are querying for integers 123 and 481334 as opposed to strings which would be correct looking at your sample data. I'm going for the string version with my below code in order to make it work against the provided sample data.
You have two options:
Either you simply limit your result set but keep the same structure using a simple find + projection:
document = collection.find(and(eq("idkey", "123"), eq("objects.key1", "481334"))).projection(fields(excludeId(), include("objects.key2.key4"))).first();
Or, probably nicer in terms of output (not necessarily speed, though), you use the aggregation framework in order to really just get what you want:
document = collection.aggregate(Arrays.asList(match(and(eq("idkey", "123"), eq("objects.key1", "481334"))), unwind("$objects"), project(fields(excludeId(), computed("key4", "$objects.key2.key4"))))).first();
I am using Jayway JsonPath 2.2 version in JAVA. I have few questions on the same.
Sample JSON:
{
"l5": [
{
"type": "type1",
"result": [
"res1"
]
},
{
"type": "type2",
"result": [
"r1"
]
}
]
}
I want to fetch a string after a filter is applied?
Eg:
Path used to fetch a string is l5[?(#.type == 'type2')].type
Expected result: type2 (string), but getting ["type2"] (array)
Please correct the path if i am doing anything wrong to fetch it as a string?
Unable to index the resultant array after the filter is applied. How can i achieve the same?
Eg:
If i use the path l5[?(#.type == 'type2')][0], instead of returning me the first JSONObject it returns []
Is it possible to extract substring from a string using jsonPath?
Something like l5[0].type[0,2] => res
Note: I cannot do any operation in JAVA, as i wanted to keep the library generic?
The return value of jsonPath is always an array, quoted from here.
So, the operation1 you are trying is not possible i.e. String value will never be returned.
Even operation 2 and operation 3 doesn't look feasible with jayway jsonpath.
Good read on jayway Jsonpath.
Only Possible way for operation 1 and 2 looks something like below:-
String jsonpath="l5[?(#.type == 'type2')].type";
DocumentContext jsonContext = JsonPath.parse(jsonString);
List<String> typeDataList= jsonContext.read(jsonpath);
System.out.println(typeDataList.get(0)); //type2
I'm trying to implement application that get document from MongoDB and insert it to ElasticSearch. Here is a piece of code that should insert document to the ElasticSearch index:
final Document o = (Document) document.get("o"); // this is where object lives
client.prepareIndex(index, mapping, id.toString())
.setSource(o.toJson())
.execute().actionGet();
And finally I get this error:
java.lang.IllegalArgumentException: Mapper for [title] conflicts with existing mapping in other types:
[mapper [title] has different [store_term_vector] values, mapper [title] has different [store_term_vector_offsets] values, mapper [title] has different [store_term_vector_positions] values, mapper [title] has different [store_term_vector_payloads] values]
at org.elasticsearch.index.mapper.FieldTypeLookup.checkCompatibility(FieldTypeLookup.java:117)
at org.elasticsearch.index.mapper.MapperService.checkNewMappersCompatibility(MapperService.java:368)
at org.elasticsearch.index.mapper.MapperService.merge(MapperService.java:319)
I'v tried to remove index completely using XDELETE and recreate using XPUT and error remain.
Here is how my index settings look like:
{
"msg": {
"mappings": {
"Message": {
"properties": {
"title": {
"type": "string",
"term_vector": "with_positions_offsets_payloads",
"analyzer": "russian"
}
}
}
}
}
}
However if I remove this term_vector part from index settings the code is inserts new document successfully.
Can someone explain me what is the problem? The same problem occur when I'm trying to use mongo-connector. If settings contain term_vector part for title field => mongo-connector fails with same Exception. And it works well without term_vector.
Are you sure you are using the correct term_vector value? I am only aware of five valid values for that attribute, as listed in the documentation:
Possible values are no, yes, with_offsets, with_positions, with_positions_offsets. Defaults to no.
I would suggest trying a different term_vector such as with_positions_offsets to see if you get the results you're expecting.
I hope my answer will help someone else.
The problem was that I have another mapping in the same index that also have field title. You have to update all other mappings to use same settings.