Filter by Date with Spring Data MongoTemplate - spring

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"),

Related

How to pass object type argument in query in GraphQL?

I got this type of query
query {
searchRandom (param : MyObjectClass){
city
}
}
How may I set param with the type of MyObjectClass and pass it in the query? To be able to test here?
Use the following query.
query getData($param: MyObjectClass){
searchRandom(param: $param)
city
}
And then go to query variables tab in Graphiql and pass the variable data like this. You have not mention the data types included in MyObjectClass. So use this as an example:
{
"param": {"country": "England", "population": "High" }
}
Then the data should be returned as expected.
--- Additionally ---
If you are running the server, make sure you have set the followings.
You need to create a input object in the GraphQL schema.
input MyObjectClass {
country: String
population: String
}
Then in the resolver you have to pass the object as the argument. (Assuming you are using JavaScript)
const resolvers = {
Query: {
searchRandom: (parent, { param }) => {
var query_data = param
...//your code
return city_name;
},
},
I am not sure whether this addresses your question or not. I hope this answer helps though.

How does GraphQL support AND-OR query conditions?

I'm new to GraphQL. There is a requirement that query all the qualified data from CUSTOMERS table, which meets condition: GENDER == 'MALE' or 'AGE' >= 20. What GQL should looks like for it?
Someone proposed a similar thing before but it is rejected. That means GraphQL does not support it natively and you have to roll it out by yourself.
Several ways to do it based on what I see :
(1) Define your own query language such as what stackoverflow or Shopify does :
type Query{
customers (query:String) : [Customer]
}
The query becomes :
{
customers (query : "GENDER == 'MALE' or 'AGE' >= 20"){
id
name
}
}
(2) Define your own input object models that can cover all the required searching requirement.Prisma try to define one in OpenCRUD specification .You may take a look on it for the idea . For example , you can define an input model like :
input CustomerFilter {
AND : [CustomerFilter]
OR : [CustomerFilter]
# Define the fields that you support to search
gender : String
gender_not : String
....
....
...
...
age : Int
age_gte : Int
}
type Query{
customers (filter:CustomerFilter) : [Customer]
}
And the query becomes :
{
customers (filter : {
OR: [
{ gender : 'MALE' } ,
{ age_gte: 20 }
]
}){
id
name
}
}
This is another filter model for reference. The idea is to tailor-made it such that it is just enough to handle all your application requirements without introducing any unnecessary filtering complexity.
Also , you most probably need to consider something like pagination if it potentially will return many data. It means you have to add an offset and limit to the input arguments for each query to somehow limit the number of record returned if you are doing offset-based pagination or take a look on Relay Specification if you want to do it in the cursor-based pagination style.
You need to define query, schema and resolver. Your Query will be like :
type Query {
nameItAsPerUsecase: Customer
}
Schema will be like these :
type Customer{
name : String
age: Int
.. add more fields as per as your need
}
Your resolver will be like this: -
#Component
public class CustomerQuery implements GraphQLQueryResolver {
#Autowired
private CustomerRepository customerRepository ;
public Customer getNameItAsPerUsecase() {
return customerRepository.findByGenderAndAgeGreaterThanEqual(String gender, int age);
}
}
Nested logic with the same and/or conjunction can be simplified into a single list.
For example, the following complex query:
or: [
{ or: [ { foo: { eq: "A" } }, { bar: { eq: "B" } } ] },
{ or: [ { baz: { eq: "C" } }, { quz: { eq: "D" } } ] }
]
} ) { ... }
Moreover, can be simplified into the following simplified query syntax:
queryPost(filter: {
or: [
{ foo: { eq: "A" } },
{ bar: { eq: "B" } },
{ baz: { eq: "C" } },
{ quz: { eq: "D" } }
]
} ) { ... }

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()

Spring mongodb ArrayOperators filter by $in clause

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"]

How to index an object with completion fields

Following http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-completion.html
How can I index/insert (I can do mapping) an object using Nest client library to be able to provide following options:
"input": ...,
"output": ...,
"payload" : ...,
"weight" : ...
I would like to be able to provide multiple values in 'input' option.
Can't find anyway of doing this using NEST.
Thank you
NEST provides the SuggestField type in order to assist in indexing completion suggestions. You don't necessarily need to use this type, you can provide your own that contains the expected completion fields (input, output, etc...), but the purpose of SuggestField is to make the whole process easier by already providing a baked in type.
Usage:
Add a suggest field to the document/type you are indexing:
public class MyType
{
public SuggestField Suggest { get; set; }
}
Your mapping should look something like:
client.Map<MyType>(m => m
.Properties(ps => ps
.Completion(c => c.Name(x => x.Suggest).Payloads(true))
)
);
Indexing example:
var myType = new MyType
{
Suggest = new SuggestField
{
Input = new [] { "Nevermind", "Nirvana" },
Output = "Nirvana - Nevermind",
Payload = new { id = 1234 },
Weight = 34
}
};
client.Index<MyType>(myType);
Hope that helps.

Resources