0

The command "rdb.table('newnmeadata').orderBy('nos')" works fine in the RethinkDB data explorer. 'Nos' is the primary key. There are no problems with getting data (for a socket feed) using ".with_fields" but the RethinkDB serves the data in any old order. I have tried many many iterations of the below but am now stumped?

import rethinkdb as r
rdb = r.RethinkDB()
rdb.connect('localhost', 28015).repl()

while True:

    cursor = rdb.table('newnmeadata').orderBy('nos').run()
    for document in cursor:

        msg = (str(document)[10:-13])
        print(msg)

Produces the following error:

Python 3.8.1 (C:/Program Files (x86)/Python/python.exe) %Run datastream.py Traceback (most recent call last): File "C:\Program Files (x86)\Python\MyPy\datastream.py", line 7, in cursor = rdb.table('newnmeadata').orderBy('nos').run() AttributeError: 'Table' object has no attribute 'orderBy'

2 Answers2

1

Thanks order_by worked in python :) My Command:

QueryData = r.db('myDB').table('Posts').order_by(r.desc('Data_Time')).run()

works Well Thanks again :)

  • Instead of thanking yourself multiple times like a teenage in midlife crisis, you can add more explanation to your solution (How it solve OP problem). SO, anybody who see this post or anybody who run into same problem as OP can easily find a solution. Because, you know... some of us are procrastinator. – Willy satrio nugroho Dec 16 '20 at 07:38
0

It should be order_by for python rethinkdb driver (and orderBy is for javascript). Like this:

cursor = rdb.table('newnmeadata').order_by('nos').run()
inf581
  • 612
  • 1
  • 7
  • 9
  • Yup that worked - thank you so much. Massive delay while it does the sort for the order_by - not sure what you can do to improve that? 'nos' is the primary key and the data was loaded into the table in that order i.e. 1.....n yet it's not stored that way it would seem - odd? – Bertie Bravage Jan 12 '20 at 18:19
  • Sorting without index requires rdb server to hold the entire sequence in memory, so try to use sorting with index, like this `r.table('newnmeadata').order_by(index='nos')`, it should be better. – inf581 Jan 13 '20 at 08:15
  • That is much quicker - thank you. Interestingly as I only wanted the 'data' field I added a with_fields filter but that has to come after the order_by, not before else you get "rethinkdb.errors.ReqlQueryLogicError: Indexed order_by can only be performed on a TABLE or TABLE_SLICE in:" - logical enough I suppose – Bertie Bravage Jan 13 '20 at 08:50