Chris Seufert
Chris Seufert

Reputation: 869

Retrieving documents in order they were inserted

I am wanting to know how to create an index in rethinkdb that will return rows in the order they were added, to use it as a kind of log.

Upvotes: 1

Views: 208

Answers (1)

dalanmiller
dalanmiller

Reputation: 3662

You will want to set a datetime field of some sort in your documents like so:

# Shorthand for table
test = r.db("test").table("test")

# Create index
test.createIndex("datetime", r.row("datetime"))

# Insert document with datetime field
test.insert({
 datetime: r.now(),
})

# To get all documents in sorted order
test.order_by(index="datetime")

# To get documents after a certain point
test.between(<some sort of datetime object>, r.maxval, index="datetime")

Upvotes: 2

Related Questions