Entity Framework core: sum of sum doesn't work - entity-framework

I'm trying to use this query using EF core:
entity.OrderBy(e => e.FieldOne.Sum(fo => fo.NestedField.Sum(nf => nf.SecondNestedField)));
Error:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery. Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
Is it possible to rewrite it EF core way?

It's sound not possible to call a complex expression in Sum. But you can flat the list and do a simple Sum like :
entity.OrderBy(e.FieldOne.SelectMany(fo => fo.NestedField).Sum(nf => nf.SecondNestedField));

Related

Match and merge MongoDB fields with specific values in aggregation

Given: objects in DB can have field 'name'. I need to aggregate all the obj with 'name' equals 'Theodor', which is easy.
But how do I also match all the objects with 'name' equals 'Ted', and treat them same way as 'Theodor' later in aggregation pipeline? Sort of merge both of these groups into one.

How to build complex filter query, using aggregation. MongoDb

I have a collection of "evaluations", each evaluation with the following structure:
And I have a list of labels to filters with this sample structure:
[{ LabelId="5fe34b13f0031e1078e08b5c", Value="BOL"},
{ LabelId="5fe34b13f0031e1078e08b5c", Value="LGV"}]
What I want to do is filter "evaluations" collection to get only the evaluations that have EvaluationResults that have EvaluatedLabels that match the same LabelId and Value of the object filters in the list above.
In other words, I need the evaluations that evaluated the label/value combinations given.
How would I do that with mongodb query language using aggregation framework?
You can create compound index on both fields as follow:
db.evaluations.createIndex({ "EvaluationResults.EvaluationLabels.LabelId":1, "EvaluationResults.EvaluationLabels.Value":1 })
and filter for specific evaluation as follow:
db.evaluations.find({ "EvaluationResults.EvaluationLabels.LabelId":"5fe", "EvaluationResults.EvaluationLabels.Value":"BOL" })

Spring Data Mongo Query to query with multiple fields and return in one call

Firstly, the document schema am querying on, is as follows:
{x:10, y:"temp", z:20}
There are multiple other documents in the collection with the same schema as above.
Now, I have a list where each element contains, pair of values belonging to keys x and y. This can be pictured as:
[{10,"temp"}, {20,"temp1"}, .....]
keys -> x y x y
Now, am aware, that if I process the array in a loop and take each pair, I can construct a query like:
query.addCriteria(Criteria.where("x").is(10).and("y").is("temp"))
This will return the document if it matches the AND criteria. I can query with all the pairs in the list in such a manner. But this approach will involve a high number of calls to the data base since for each pair in the list, there is a database call.
To avoid this, Is there any way I can query for all the documents that match this AND criteria for each element in the list, in a single call, using spring data MongoDb Api? Framed differently, I want to avoid looping through the array and making multiple calls, if possible.
You could use Criteria.orOperator to return each Document that match at least one Criteria of your list.
Build your list of Criteria looping over your list
List<Criteria> criteriaList = new ArrayList<>();
for (item : yourList) {
criteriaList.add(Criteria.where("x").is(item.x).and("y").is(item.y));
}
Build your query using orOperator:
Query.query(new Criteria.orOperator(criteriaList.toArray(new Criteria[criteriaList.size()])));

JPA Criteria in clause with tuple expression

Looking to translate an HQL query into it's JPA Criteria API equivalent.
In HQL I have the following which seems to work.
WHERE (x, y) IN (
... sub query which selects two columns
)
In JPA Criteria API I don't see how I can match a tuple value, only a single value. For example, I know how to convert the following to JPA Criteria.
WHERE (x) IN (
... sub query which selects single column
)
Is this even possible using the criteria api?
It's not possible in JPQL to try to match two fields with the IN.
By the way, you can't do it in JPA Criteria neither.

can we use find query inside map in mongo

I need to perform some aggregation on one existing table and then use aggregated table to perform the map reduce.
The aggregation table is sort of a temporary used so that it can be used in map reduce. Record set in temporary table reaches around 8M.
What can be the way to avoid the temporary table?
One way could be to write find() query inside map() function and emit the aggregated result(initially being stored on aggregation table).
However, I am not able to implement this.
Is there a way! Please help.
You can use the "query" parameter on MongoDB MapReduce. With this parameter the data sent to map function is filtered before processing.
More info on MapReduce documentation