DEV Community

Rodion Gorkovenko
Rodion Gorkovenko

Posted on

Rewrite this SQL for NoSQL, graph-db (e.g. Neo4j)

I'm trying to recollect some examples of SQL queries which I couldn't rewrite into queries for graph database some years ago. Then it was Orient, but for now, probably, let's assume Neo4j is used (or other NoSQL database you believe is suitable).

Here is one example, but I'm not sure. Please tell if it fits some NoSQL database (supposedly, graph db) nicely or not. Also feel free please to suggest your own queries to attempt translation, so we can think together.

-- mysql 5.6 or alike

create table users(id int, name text);
create table rooms(id int, label text);

insert into users values (1, 'Peter'), (2, 'Slava'),
  (3, 'Margo'), (4, 'Clown'), (5, 'Pedros');
insert into rooms values (1, 'alpha'), (2, 'beta');

-- now let's assign users to rooms according to modulo of their id

select concat(name, ' in ', label)
    from users u join rooms r
    on u.id % 2 + 1 = r.id;

This is a bit artificial, but the main idea is this - I need join, but on something which doesn't have meaning of real relation. E.g. users could be assigned to such rooms today, but tomorrow the 3-rd room is available and they are reassigned by mod 3. I believe when I had difficulties with project, it was about assigning transactions to various periods and categories or something like this - don't remember well, regretfully - but it should be roughly the same...

Top comments (0)