Compound sort not working in Spring mongodb - spring-data-mongodb

I have a requirement where the records will be sorted based on created date first and if created dates are same, we will sort on another field called as ratings.
In my Spring mongo project I am doing the following thing:
Query query = new Query();
query.with(new Sort(Direction.DESC, "crDate")).with(new Sort(Direction.DESC, "ratings"));
For some reasons its only sorting on the first field ie crDate. And if both dates are same, sort by ratings never work.
When i try to check the value of sort object it shows me this:
{"crDate":-1,"ratings":-1}
Another finding is, mongo takes in the following syntax for compound sorts:
db.abc.find({..some criteria..}).sort([["crDate",-1],["ratings",-1]]);
Is this a bug in spring mongodb implementation or I missed something?

Looking at the Spring API Documentation it shows you can specify multiple strings to the sort object you are creating in a list. From you snippet above I would suggest you need to only apply the one sort object that takes the two fields, something like
query.with(new Sort(Direction.DESC, Arrays.asList("crDate", "ratings")));

There was another constructor that took the List of Order objects. Strange but I tried it with that now and it seems to be working.
I am now using a single with clause and passing in a List of Order

Related

Google Sheets Filtering by 2 separate conditions

I'm trying to create a way to most effectively see who is working at a certain day and block of time. So far, I've created my data lists that have the Name of the reps in A, the day available in B, the reps names again in D, and the blocks of time available in E. I've been trying to use FILTER in order for it to pull based on 2 dropdowns. I've tried using all 3 of these:
=filter(Sheet2!A2:A999,Sheet2!B2:B999=A2,Sheet2!E2:E999=B2)
=FILTER(Sheet2!A2:A,(Sheet2!B1:b=A2)+(Sheet2!B2:B=B2)
=Filter(Filter(Sheet2!A2:A999, Sheet2!B2:B999=A2), Sheet2!B2:B999=B2)
But I can't crack it. The last nested formula seems like it's functioning correctly, except that it's returning a different number of rows each time, so I don't know how to avoid the mismatched range error it gets. To be fair, I'm basically trying to create this from scratch, having known nothing about the FILTER function before saturday. Any ideas on how I can accomplish this with filters would be super helpful.
https://docs.google.com/spreadsheets/d/1WhBSQy4OZFtJvheNHd5ZfIRvcDxvUb-WhdY0mUci3O0/edit?usp=sharing
I have found this function =filter(Sheet2!A2:A136, Sheet2!B2:B136 =G2) on your sheet and based on that I have added the filter for the time, and works.
The working condition is:
=filter(Sheet2!A2:A136, Sheet2!B2:B136 =A2, Sheet2!E2:E136 = B2)
Based on the FILTER documentation:
FILTER(range, condition1, [condition2, ...])
first, in your data validation fix 7-7:30 to 7-730. then use this:
=ARRAYFORMULA(UNIQUE(QUERY(TO_TEXT({Sheet2!A2:B; Sheet2!D2:E}),
"select Col1 where Col2 matches '"&TEXTJOIN("|", 1, A2:B2)&"'")))

How to filter based on the last element in ArrayField in django

I'm using postgresql database which allows having an array datatype, in addition django provides PostgreSQL specific model fields for that.
My question is how can I filter objects based on the last element of the array?
class Example(models.Model):
tags = ArrayField(models.CharField(...))
example = Example.objects.create(tags=['tag1', 'tag2', 'tag3']
example_tag3 = Example.objects.filter(tags__2='tag3')
I want to filter but don't know what is the size of the tags. Is there any dynamic filtering something like:
example_tag3 = Example.objects.filter(tags__last='tag3')
I don't think there is a way to do that without "killing the performance" other than using raw SQL (see this). But you should avoid doing things like this, from the doc:
Tip: Arrays are not sets; searching for specific array elements can be
a sign of database misdesign. Consider using a separate table with a
row for each item that would be an array element. This will be easier
to search, and is likely to scale better for a large number of
elements.
Adding to the above answer and comment, if changing the table structure isn't an option, you may filter your query based on the first element in an array by using field__0:
example_tag3 = Example.objects.filter(tags__0='tag1')
However, I don't see a way to access the last element directly in the documentation.

Django - get queryset with id's not in set of values

According to doc -
https://docs.djangoproject.com/en/dev/topics/db/queries/#the-pk-lookup-shortcut -
I can get set of objects with specified in list ids. Is there any short way to get another set of objects, with id's not in the specified list. Blog.objects.filter(pk__not_in=[1,4,7]) - did not work for me. PS: is there any annotation of possible expresissions for filtering querysets, of making own short expressions?
Use the exclude method.
Blog.objects.exclude(pk__in=[1,4,7])
At first your query is wrong. You should write your query Blog.objects.filter(pk__in=[1,4,7]). And if you want to use not then you should read here

Paging arrays in mongodb subdocument

I have a mongo collection with documents that have a schema structured like the following:
{ _id : bla,
fname : foo,
lname : bar,
subdocs [ { subdocname : doc1
field1 : one
field2 : two
potentially_huge_array : [...]
}, ...
]
}
I'm using the ruby mongo driver that currently does not support elemMatch. I use an aggregation when extracting from subdocs via a project, unwind and match pipeline.
What I would now like to do is to page results from the potentially_huge_array array contained in the subdocument. I have not been able to figure out how to grab just a subset of the array without dragging the entire subdoc, huge array and all, out of the db into my app.
Is there some way to do this?
Would a different schema be a better way to handle this?
Depending on how huge is huge, you definitely don't want it embedded into another document.
The main reason is that unless you always want the array returned with the document, you probably don't want to store it as part of the document. How you can store it in another collection would depend on exactly how you want to access it.
Reviewing the types of queries you most often perform on your data will usually suggest the best schema - one that will allow you to be efficient about number of queries, the amount of data returned and ease of indexing the data.
If you field really huge and changes often, just placed it in separate collection.

Working with columns having dot(.) in their name using GQL

I use Objectify for datastore operations in my GAE/Java application. I have used Objectify's #Embeded facility in a couple of places in my project. Objectify automatically flattens the nested objects within the entity marked by #Embeded notation using the . separator. Thus I have ended up with column names like entity.embededObject.Field
For example I have an entity 'Person' in my data store with two columns name and address.email.
I want to filter through Person in the datastore viewer by writing a simple GQL query.
But the following query fails with a syntax error:
SELECT * FROM Person where address.email='mail#gmail.com'
whereas the following works as it should
SELECT * FROM Person where name='Joe'
What am I doing wrong?
GQL currently doesn't support this - only 'word' characters are supported. You should definitely file this as a bug in the issue tracker.
Tested today, it is possible to run the following with backquotes
SELECT * FROM `your.kind`
I believe this holds true for any parameter, but please correct me if I am wrong.

Resources