Konkit Blog

Konkit Blog




🔞 ALL INFORMATION CLICK HERE 👈🏻👈🏻👈🏻

































Konkit Blog
{ "_id" : ObjectId(...) , "enumField" : "FirstValue" } ,
{ "_id" : ObjectId(...) , "enumField" : "LastValue" } ,
{ "_id" : ObjectId(...) , "enumField" : "MiddleValue" } ,
{ "_id" : ObjectId(...) , "enumField" : "FirstValue" }

enumOrder = [ "FirstValue" , "MiddleValue" , "LastValue" ]

m = { "$match" : { "enumField" : { "$in" : enumOrder } } };
a = { "$addFields" : { "__order" : { "$indexOfArray" : [ enumOrder , "$enumField" ] } } };
s = { "$sort" : { "__order" : 1 } };
db . people . aggregate ( [ m , a , s ] );

{ "_id" : ObjectId (...), "enumField" : "FirstValue" , "__order" : 1 },
{ "_id" : ObjectId (...), "enumField" : "LastValue" , "__order" : 3 }
{ "_id" : ObjectId (...), "enumField" : "MiddleValue" , "__order" : 2 }
{ "_id" : ObjectId (...), "enumField" : "FirstValue" , "__order" : 1 }

FindIterable < Document > iterable1 = collection . find ( Filters . eq ( "enumValue" , EnumType . FIRST_VALUE . toString ()));
FindIterable & lt ; Document & gt ; iterable2 = collection . find ( Filters . eq ( "enumValue" , EnumType . MIDDLE_VALUE . toString ()));
FindIterable & lt ; Document & gt ; iterable3 = collection . find ( Filters . eq ( "enumValue" , EnumType . LAST_VALUE . toString ()));

Iterable & lt ; Document & gt ; result = Iterables . concat ( iterable1 , iterable2 , iterable3 );

You could think that sorting by an enum value is a routine operation in MongoDB, but if we are interested in some custom order, suddenly it’s not so trivial anymore. In SQL databases, the information about how enum values compare to each other is stored in the schema. Since there are no schemas in MongoDB, we have to find other solutions.
I had a collection with ~10 million documents and I wanted to present the data in a table with paging functionality.
Moreover, this table had sortable fields, and one of those was of an enum type.
Obviously, I didn’t want to fetch all 10 million documents just to display 10 of them.
Paging itself wasn’t hard to implement.
The find()
operation in Java Mongo driver returns the object implementing the Iterable interface, so it’s a great fit for tools we have in Java.
This, however, made the easiest approach impossible, which was to sort the data within the application.
It was necessary to involve MongoDB in the process of sorting, but since MongoDB doesn’t have any schema,
I had to pass the order of enum values in some other way.
The obvious choice would be to serialize the enum values as numbers.
Unfortunately, this comes with a disadvantage if we would need to modify the enum hierarchy, e.g. by adding a field in the middle.
To do that, we would have to migrate the whole collection and change the values accordingly.
A different solution is to use the aggregation framework. It is very well explained in the blog post by Asya Kamsky called
Stupid tricks with MongoDB – Using 3.4 Aggregation to Return Documents in Same Order as “$in” Expression
The author uses the $addFields stage, where we could add a numeric field to our results representing the enum value in the hierarchy.
So for such documents:
we would have to run the following query:
As an intermediate step, the query above adds the __order field to our data, so it looks like this:
and the last step simply sorts by the added numeric field.
This is better from the previous approach, because the order is passed in the query itself,
so potential change in code does not require any data migration.
Unfortunately, for large amount of documents this approach haven’t performed well.
Because of that, I could make 3 separate queries, each with a different value of the enum, and concatenate them into one Iterable.
This could be achieved e. g. by Iterables.concat from Guava.
So … it would look like somewhat like this:
When we run the program, we can see in the logs, that the queries aren’t being run immediately,
but instead, the first query is executed, the data is fetched, and only then the next query is executed in the database.
If it was helpful in any way or if you have any other idea how to tackle this problem, make sure to let me know in the comment below !

Konkit's Tech Blog by Łukasz Tenerowicz.


Something went wrong, but don’t fret — let’s give it another shot.


# linux
# bash
# zsh
# productivity


# productivity
# email
# tips
# inboxzero


# ruby
# cli
# productivity


We're a place where coders share, stay up-to-date and grow their careers.


Posted on Jun 18, 2020


• Originally published at konkit.tech on May 21, 2020

For a very long time, I’ve been using just tab , ctrl-C , ctrl-D and ctrl-R shortcuts. However, there are more useful gems out there. In this post, I’m going to show you Bash/Zsh terminal shortcuts that I find useful.
It’s the most important shortcut because it’s the autocompletion. It simply saves you a whole lot of typing. If there is just one possible option, then it will be automatically filled for you. If not, possible options will be presented below.
Ctrl-C terminates the currently running command or a process. Ctrl-D is an equivalent of exit command. You can think about the Python REPL, where Ctrl-C ends the currently running command, but Ctrl-D exits the REPL itself.
Ctrl-C sends a SIGINT signal, to a process, which is currently in the foreground. Application usually interpret this as a signal to kill the process, but they don’t have to do so. This is why Python interpreter doesn’t end the process, but just ends the command that is currently running.
Ctrl-D sends an End-Of-File marker. To explain this, imagine that we run a Python script. The interpreter reads the program from a file and executes commands until the whole file is read. When it reaches the end of the file, the interpreter terminates.
When we run the interpreter without any script, our keyboard input is treated as a “file”, from which the commands are read. When we push Ctrl-D , we inform the interpreter, that it reached the end of the file, all commands are read and thus it can terminate.
Of course it doesn’t have to be a Python interpreter – the same goes for other interpreted languages or shells like bash. Try to run bash inside a bash – you can “go back” with Ctrl-D shortcut.
When you have some long-running command, you don’t necessarily have to terminate it if you have to do something else. When you press Ctrl-Z , the current process goes to the background. You no longer see its output, but the process is still running.
If we want to bring back that process to the foreground, we can use the fg command to do that.
We may also pause the output for a moment. This is useful, when there is a lot of fast-changing output, like application logs we would like to inspect. To pause the output we can use the shortcut Ctrl-S (like s top). To resume it back, use the Ctrl-Q .
To run a previous command, we just need to press the up arrow. With up and down arrows we can navigate between already executed commands.
The same effect can be achieved using Ctrl-P (like in p revious command) and Ctrl-N (like in n ext command). At first, arrow keys may be more intuitive, but as you become more proficient with keyboard, it may be more efficient to use the latter ones, because you can keep your hands on the letter keys.
Arrow keys are not convenient to use if the command was executed some time ago. To search in history for a command, we can use a Ctrl-R shortcut.
To display whole saved history, we can also use a history command.
Ctrl-A is basically an equivalent of the Home button. It moves the caret to the beginning of the line.
Ctrl-E is like the End button. It moves the caret to the end of a line.
To clear the screen and go back on top of our terminal screen, we can use the clear command, but we can also do it faster, using the Ctrl-L shortcut. You know – c L ear (not very intuitive, but it’s something …)
If we make a mistake in a last written word, we can use this shortcut to quickly delete the last word.
When we would like to remove a whole line, we can use Ctrl-U shortcut (like in u ndo) to clear the whole line.
Ctrl-K shortcut deletes characters from the cursor to the end of the line.
Now, if we remove a command or its part by either of Ctrl-K , Ctrl-U or Ctrl-W shortcuts, we can paste it via Ctrl-Y shortcut (like in y ank). It’s very useful when you write a very long command, but then you realize, that you have to run another one first.
Did I miss any shortcut that should be on that list? Let me know in the comment!
Templates let you quickly answer FAQs or store snippets for re-use.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink .

For further actions, you may consider blocking this person and/or reporting abuse

Once suspended, konkit will not be able to comment or publish posts until their suspension is removed.


Once unsuspended, konkit will be able to comment and publish posts again.


Once unpublished, all posts by konkit will become hidden and only accessible to themselves.


If konkit is not suspended, they can still re-publish their posts from their dashboard.


Once unpublished, this post will become invisible to the public
and only accessible to Łukasz Tenerowicz.

They can still re-publish the post if they are not suspended.

DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey.

Built on Forem — the open source software that powers DEV and other inclusive communities.
Made with love and Ruby on Rails . DEV Community © 2016 - 2022.

We're a place where coders share, stay up-to-date and grow their careers.






Features




Mobile




Actions




Codespaces




Copilot




Packages




Security




Code review




Issues




Discussions




Integrations




GitHub Sponsors




Customer stories








Explore GitHub


Learn and contribute



Topics




Collections




Trending




Skills




GitHub Sponsors




Open source guides


Connect with others



The ReadME Project




Events




Community forum




GitHub Education




GitHub Stars program








Plans




Compare plans




Contact Sales




Education






In this user


All GitHub





In this user


All GitHub





Search


All GitHub





In this user


All GitHub






Overview


Repositories


Projects


Packages


Stars







Łukasz Tenerowicz


konkit







Overview


Repositories


Projects


Packages


Stars









web-push-libs / webpush-java
Public






jenkinsci / jira-steps-plugin
Public


Less















More




2022


2021


2020


2019


2018


2017


2016


2015


2014


2013





konkit has no activity
yet for this period.











You can’t perform that action at this time.





You signed in with another tab or window. Reload to refresh your session.
You signed out in another tab or window. Reload to refresh your session.


Prevent this user from interacting with your repositories and sending you notifications.
Learn more about blocking users .


You must be logged in to block users.


Contact GitHub support about this user’s behavior.
Learn more about reporting abuse .


Ordering food at your office made simple.


Spaceshooter game in c++ being built with Ogre3d as an assignment for university.


Jenkins pipeline steps for integration with JIRA.


Seeing something unexpected? Take a look at the
GitHub profile guide .


Rose Red Sex
Mexican Anal Gif
Porn Star Valentina Nappi

Report Page