Spring mongodb ArrayOperators filter by $in clause - spring

I have to convert this mongodb aggregate to spring aggregate code
{ $addFields: {
"versions" : { $filter: {
input: "$versions",
as: "version",
cond: {$in: ["$$version.parentId", "$libraries._id"]}
}}
}}
My current code is
AddFieldsOperation.with("versions",
ArrayOperators.arrayOf("versions").filter().as("version").by(
ArrayOperators.In.arrayOf("version.parentId").containsValue(
ArrayOperators.arrayOf("libraries._id")
)
)
)
but this throws this error:
$in requires an array as a second argument, found: objectId'
I assume it's because it's not accepting the argument
ArrayOperators.arrayOf("libraries._id")

If we take a look at the Spring documentation for ArrayOperators, we find the following method definition:
arrayOf
public static ArrayOperators.ArrayOperatorFactory arrayOf(String fieldReference)
Take the array referenced by given fieldReference.
Parameters:
fieldReference - must not be null.
Returns:
Notice that it states that fieldReference should reference an "array". It seems likely that you want to change ArrayOperators.arrayOf("libraries._id") to ArrayOperators.arrayOf("libraries") instead.

I had query like
{
coupons: {
$filter: {
input: "$coupons",
as: "coupon",
cond: {
$in: [ "$$coupon._id", ["ABC", "DEF"] ]
}
}
}
}
Below solution worked for me
filter("$coupons").as("coupon").by(ArrayOperators.In.arrayOf(couponIds).containsValue("$$coupon._id"))
Note: couponIds is List with values ["ABC", "DEF"]

Related

I want to update values ​in an array in an array while using MongoTemplate

First, I will show the state stored in mongodb.
As you can see, it is a structure with a list called replies in a list called comments. And inside replies there is an array called likes.
comments : [
Object1 : {
replies : [
likes : [
0 : {},
1 : {}
]
]
},
Object2 : {
replies : [
likes : [
0 : {},
1 : {}
]
]
}
]
What I want to do here is to insert/subtract a value only from the likes array inside a specific replies structure. I'm currently using Spring boot and have tried the following:
Query query = new Query();
Criteria criteria = Criteria.where("_id").is(new ObjectId(postId))
.andOperator(Criteria.where("comments")
.elemMatch(Criteria.where("_id").is(new ObjectId(commentId))
.andOperator(Criteria.where("replies")
.elemMatch(Criteria.where("_id").is(new ObjectId(replyId)))
)
)
);
query.addCriteria(criteria);
Update update = new Update();
if (state) {
// remove user id
update.pull("comments.$[].replies.$.likes", new ObjectId(userId));
} else {
// add user id
update.push("comments.$[].replies.$.likes").value(new ObjectId(userId));
}
mongoTemplate.updateFirst(query, update, MyEntity.class);
It is an operation to add or remove userId according to boolean state. As a result of the attempt, up to a specific comment is found, but userId is unconditionally entered in the first likes list of the replies list inside the comment. What I want is to get into the likes list inside a specific reply. Am I using the wrong parameter in update.push()? I would appreciate it if you could tell me how to solve it.
Not a direct answer to your question as I'm not experienced with spring's criteria builder, but here's how you would do it in mongo directly, which might help you to figure it out:
You could define arrayfilters allowing you to keep track of the corresponding indices of each comments and replies. You can then use those indices to push a new object at the exact matching indices:
db.collection.update({
_id: "<postId>"
},
{
$push: {
"comments.$[comments].replies.$[replies].likes": {
_id: "newlyInsertedLikeId"
}
}
},
{
arrayFilters: [
{
"comments._id": "<commentId>"
},
{
"replies._id": "<replyId>"
}
]
})
Here's an example on mongoplayground: https://mongoplayground.net/p/eNdDXXlyi2X

Filter by Date with Spring Data MongoTemplate

I would like to filter my filedA's array by dates, with a mongo query it looks like that :
{
$project: {
user: "$$ROOT",
fieldA: {
$filter: {
input: "$fieldA",
as: "a",
cond: {
$and: [
{$lt: ["$$a.constraint", new Date()]},
{$gt: ["$$a.constraint", new Date()]}
]
}
}
}
}
},
The query works but I have trouble when I tried to do it with spring :
project()
.and("$$ROOT").as("user")
.and(
filter("$fieldA")
.as("a")
.by(
and(
ComparisonOperators.Lte.valueOf("a.constraint")
.lessThanEqualTo(dateEnd),
ComparisonOperators.Gte.valueOf("a.constraint")
.greaterThanEqualTo(dateStart)
)
)).as("fieldA"),
I think this is not the right way to make dates comparations but I don't know how to do it properly. Could help me figure out what I'm doing wrong?
try wrapping your dateStart and dateEnd variables in ConvertOperators.ToDate.toDate.
The ComparisonOperators methods (in this case lessThanEqualTo and greaterThanEqualTo) accept either String or AggregationExpression.
By using the ConvertOperators.ToDate.toDate method, you ensure that the passed argument is AggregationExpression and the value is correctly formatted for comparing.
Result would look like this:
import static org.springframework.data.mongodb.core.aggregation.ConvertOperators.ToDate.toDate
...
project()
.and("$$ROOT").as("user")
.and(
filter("$fieldA")
.as("a")
.by(
and(
ComparisonOperators.Lte.valueOf("a.constraint")
.lessThanEqualTo(toDate(dateEnd)),
ComparisonOperators.Gte.valueOf("a.constraint")
.greaterThanEqualTo(toDate(dateStart))
)
)).as("fieldA"),

How to add a json in a nested array of a mongodb document using Spring?

Document stored in mongodb:
{
"CNF_SERVICE_ID":"1",
"SERVICE_CATEGORY":"COMMON_SERVICE",
"SERVICES":[{
"SERVICE_NAME":"Authentication Service",
"VERSIONS":[{
"VERSION_NAME":"AuthenticationServiceV6_3",
"VERSION_NUMBER":"2",
"VERSION_NOTES":"test",
"RELEASE_DATE":"21-02-2020",
"OBSOLETE_DATE":"21-02-2020",
"STATUS":"Y",
"GROUPS":[{
"GROUP_NAME":"TEST GROUP",
"CREATED_DATE":"",
"NODE_NAMES":[
""
],
"CUSTOMERS":[{
"CUSTOMER_CONFIG_ID":"4",
"ACTIVATION_DATE":"21-02-2020",
"DEACTIVATION_DATE":"21-02-2020",
"STATUS":"Y"
}]
}]
}]
}
]
}
Now, I need to add another customer json to the array "CUSTOMERS" inside "GROUPS" in the same document above. The customer json would be like this:
{
"CUSTOMER_CONFIG_ID":"10",
"ACTIVATION_DATE":"16-03-2020",
"DEACTIVATION_DATE":"16-03-2021",
"STATUS":"Y"
}
I tried this:
Update update = new Update().push("SERVICES.$.VERSIONS.GROUPS.CUSTOMERS",customerdto);
mongoOperations.update(query, update, Myclass.class, "mycollection");
But, I am getting the exception: org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 28 (PathNotViable): 'Cannot create field 'GROUPS' in element
[ EDIT ADD ]
I was able to update it using the filtered positional operator. Below is the query I used:
update(
{ "SERVICE_CATEGORY":"COMMON_SERVICE", "SERVICES.SERVICE_NAME":"Authentication Service", "SERVICES.VERSIONS.VERSION_NAME":"AuthenticationServiceV6_3"},
{ $push:{"SERVICES.$[].VERSIONS.$[].GROUPS.$[].CUSTOMERS": { "CUSTOMER_CONFIG_ID":"6", "ACTIVATION_DATE":"31-03-2020", "STATUS":"Y" } } }
);
Actually, this query updated all the fields irrespective of the filter conditions. So. I tried this but I am facing syntax exception. Please help.
update(
{"SERVICE_CATEGORY":"COMMON_SERVICE"},
{"SERVICES.SERVICE_NAME":"Authentication Service"},
{"SERVICES.VERSIONS.VERSION_NAME":"AuthenticationServiceV6_3"}
{
$push:{"SERVICES.$[service].VERSIONS.$[version].GROUPS.$[group].CUSTOMERS":{
"CUSTOMER_CONFIG_ID":"6",
"ACTIVATION_DATE":"31-03-2020",
"STATUS":"Y"
}
}
},
{
multi: true,
arrayFilters: [ { $and:[{ "version.VERSION_NAME": "AuthenticationServiceV6_3"},{"service.SERVICE_NAME":"Authentication Service"},{"group.GROUP_NAME":"TEST GROUP"}]} ]
}
);
Update: April 1,2020
The code I tried:
validationquery.addCriteria(Criteria.where("SERVICE_CATEGORY").is(servicedto.getService_category()).and("SERVICES.SERVICE_NAME").is(servicedetail.getService_name()).and("SERVICES.VERSIONS.VERSION_NAME").is(version.getVersion_name()));
Update update=new Update().push("SERVICES.$[s].VERSIONS.$[v].GROUPS.$[].CUSTOMERS", customer).filterArray(Criteria.where("SERVICE_CATEGORY").is(servicedto.getService_category()).and("s.SERVICE_NAME").is(servicedetail.getService_name()).and("v.VERSION_NAME").is(version.getVersion_name()));
mongoOperations.updateMulti(validationquery, update, ServiceRegistrationDTO.class, collection, key,env);
The below exception is thrown:
ERROR com.sample.amt.mongoTemplate.MongoOperations - Exception in count(query, collectionName,key,env) :: org.springframework.dao.DataIntegrityViolationException: Error parsing array filter :: caused by :: Expected a single top-level field name, found 'SERVICE_CATEGORY' and 's'; nested exception is com.mongodb.MongoWriteException: Error parsing array filter :: caused by :: Expected a single top-level field name, found 'SERVICE_CATEGORY' and 's'
This update query adds the JSON to the nested array, "SERVICES.VERSIONS.GROUPS.CUSTOMERS", based upon the specified filter conditions. Note that your filter conditions direct the update operation to the specific array (of the nested arrays).
// JSON document to be added to the CUSTOMERS array
new_cust = {
"CUSTOMER_CONFIG_ID": "6",
"ACTIVATION_DATE": "31-03-2020",
"STATUS": "Y"
}
db.collection.update(
{
"SERVICE_CATEGORY": "COMMON_SERVICE",
"SERVICES.SERVICE_NAME": "Authentication Service",
"SERVICES.VERSIONS.VERSION_NAME": "AuthenticationServiceV6_3"
},
{
$push: { "SERVICES.$[s].VERSIONS.$[v].GROUPS.$[g].CUSTOMERS": new_cust }
},
{
multi: true,
arrayFilters: [
{ "s.SERVICE_NAME": "Authentication Service" },
{ "v.VERSION_NAME": "AuthenticationServiceV6_3" },
{ "g.GROUP_NAME": "TEST GROUP" }
]
}
);
Few things to note when updating documents with nested arrays of more than one level nesting.
Use the all positional operator $[] and the filtered positional
operator $[<identifier>], and not the $ positional operator.
With filtered positional operator specify the array filter conditions
using the arrayFilters parameter. Note that this will direct your update to target the specific nested array.
For the filtered positional operator $[<identifier>], the
identifier must begin with a lowercase letter and contain only
alphanumeric characters.
References:
Array Update
Operators
db.collection.update() with arrayFilters
Thanks to #prasad_ for providing the query. I was able to eventually convert the query successfully to code with Spring data MongoTemplate's updateMulti method. I have posted the code below:
Query validationquery = new Query();
validationquery.addCriteria(Criteria.where("SERVICE_CATEGORY").is(servicedto.getService_category()).and("SERVICES.SERVICE_NAME").is(servicedetail.getService_name()).and("SERVICES.VERSIONS.VERSION_NAME").is(version.getVersion_name()));
Update update=new Update().push("SERVICES.$[s].VERSIONS.$[v].GROUPS.$[].CUSTOMERS", customer).filterArray(Criteria.where("s.SERVICE_NAME").is(servicedetail.getService_name())).filterArray(Criteria.where("v.VERSION_NAME").is(version.getVersion_name()));
mongoOperations.updateMulti(validationquery, update, ServiceRegistrationDTO.class, collection, key,env);
mongoTemplateobj.updateMulti(validationquery, update, ServiceRegistrationDTO.class, collection, key,env);

What kind of Java type to pass into Criteria.all()?

I am trying to find a document with an array of tags which match a list of values,
using the MongoDB's $all function through Spring Data MongoDB API for all().
Tag is a embedded document with two fields: type and value.
I am not sure what kind of Java type to pass in to the method as it accepts an array of Objects, tried to pass in an array of Criteria objects into the the function but the output is below:
Query: { "tags" : { "$all" : [ { "$java" : org.springframework.data.mongodb.core.query.Criteria#5542c4fe }, { "$java" : org.springframework.data.mongodb.core.query.Criteria#5542c4fe } ] } }, Fields: { }, Sort: { }
How should I proceed?
I want to achieve the following:
db.template.find( { tags: { $all: [ {type:"tagOne", value: "valueOne"}, {type:"tagTwo", value: "valueTwo"} ] } } )
Edited for clarity:
The code which I used is similar to:
Query query = new Query(baseCriteria.and("tags").all( criteriaList.toArray()))
The criteria list is formed by:
Criteria criteria = new Criteria();
criteria.and("type").is(tag.getType()).and("value").is(tag.getValue());
criteriaList.add(criteria);
OK, I've just found out, the Java type required is org.bson.Document, which you can get using:
criteria.getCriteriaObject()

How to filter table with multiple values against one field

I am new to AWS App Sync, I want to query which accepts multiple values against one field and return result based on the input. Is this possible?
listBookByName(bookID: [String], limit: Int, nextToken: String): listBookByNameConnection
You can use DynamoDB Batch Resolvers to accomplish this.
You would attach a resolvers to the listBookByName field. The request mapping template (Takes the GraphQL query and converts it to a DynamoDB query) would look like the following:
#set($ids = [])
#foreach($id in ${ctx.args.bookID})
#set($map = {})
$util.qr($map.put("id", $util.dynamodb.toString($id)))
$util.qr($ids.add($map))
#end
{
"version" : "2018-05-29",
"operation" : "BatchGetItem",
"tables" : {
"Books": {
"keys": $util.toJson($ids),
"consistentRead": true
}
}
}
Your response mapping template will then have to marshall the results into your listBookByNameConnection type.
Here is an example in the documentation where they have batchGet query which takes a list of identifiers, and returns a list of posts.
https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-dynamodb-batch.html#single-table-batch
I have configured the setting as mentioned in the link. but it is returning null all the time.
type Query {
listBookByName(bookID: [String], limit: Int, nextToken: String): istBookByNameConnections
}
type listBookByNameConnections {
items: [Books]
}
My Resolver Mapping:
#set($ids = [])
#foreach($id in ${ctx.args.bookID})
#set($map = {})
$util.qr($map.put("id", $util.dynamodb.toString($id)))
$util.qr($ids.add($map))
#end
{
"version" : "2018-05-29",
"operation" : "BatchGetItem",
"tables" : {
"Books": {
"keys": $util.toJson($ids),
"consistentRead": true
}
}
}
Response Mapping:
$util.toJson($ctx.result.data.Books)
Query i used to check the response:
query get {
listBookByName(bookID:["JAVA","JUNIT"]){
items{
name
}
}
}
output :
{
"data": {
"listDriverTripsByName": null
}
}

Resources