Restricting nested objects results in a rethinkdb request

I would like to limit the number of nested objects inside a Rethinkdb request. Suppose I have conversations with attached messages.

[talk]

[{
    id: "fgh675",
    name: "Some conversation",
    messages: [{
        id:"jhu432",
        contents: "Hello world!",
        createdAt: "2016-01-01 00:01:01"
    },
    {
        id:"bgj876",
        contents: "Hello earth",
        createdAt: "2016-01-01 00:01:01"
    }]
}]
  • How to limit the number of message objects?

  • An event is better, how can I write a query that returns only the last message .merge(function(c) { return {msg: c("messages").slice(-1)}; }), but I cannot find how to order messages first ... (will this query be effective if there are many messages)?

+4
source share
1 answer

limit may limit the number of posts:

conversations.merge(conversation => {
  messages: conversation('messages').limit(3)
})

orderBy can be used to sort an array:

conversations.merge(conversation => {
  messages: conversation('messages').orderBy('createdAt')
})

If you sort messages by each query, it may be more efficient to store an already sorted list of messages.

+2
source

Source: https://habr.com/ru/post/1658669/


All Articles