tduccuong

tduccuong

Hi all,

I am new to Elixir and are using this lib GitHub - elixir-mongo/mongodb: MongoDB driver for Elixir · GitHub to work with MongoDB. The doc says it has “Streaming cursor” feature, but the source code implements only Enumerable protocol for the cursor returned by the Mongo.find function.

There is no doc about a cursor API, or do I miss something?

Thanks a lot!

First 10 of 11 Posts Switch mode

Ankhers

Ankhers

What is it you are trying to accomplish?

tduccuong

tduccuong OP

Thanks a lot for answering!

So, I want pagination in my app. In my website landing page, i would show first 10 items of a search query, then provide buttons to view next / previous 10 items…

I did not find an API that allows me to do that in the driver? E.g., something like: Mongo.find_next(.., cursor, ...)

Or if there is, could u pls show me?

Ankhers

Ankhers

Since this is for a website, you would not have access to the same cursor between requests. For something like this, you would structure your query like

page = 3 # This would be coming in from the URL
limit = 10 # 10 items per page
skip = (page - 1) * limit # This assume your first page is 1, so you will skip 0 for the first page, 10 for the second page, etc
options = [limit: limit, skip: skip, ...] # ... is representing any other options you want to pass in.
Mongo.find(connection, collection, query, options)

Then you are free to render the results as you see fit.

tduccuong

tduccuong OP

Thanks a lot, I will try this and give feedback…

tduccuong

tduccuong OP

confirmed that it works! thanks a lot!

Ankhers

Ankhers

Glad I could help!

tduccuong

tduccuong OP

When use this method with sort: %{<field>: <order>} option, it seems the sorting is not done in the entire result space but only on the returned batch, unless I set page = 0 and limit = <number of all items in DB>

E.g., say I have 10 items in DB labeled 1 → 10. Say an unsorted query, with limit = 10, returns

[1, 3, 2, 4, 8, 5, 9, 7, 6, 10]

Now if I set limit = 5, and sort increasing order, then with page = 0, 1, the two returned batches are:

[1, 2, 3, 4, 8]

and

[5, 6, 7, 9, 10]

What I want are:

[1, 2, 3, 4, 5]

and

[6, 7, 8, 9, 10]

Could u pls show me the way to achieve that, if there is?

Thanks a lot!

Ankhers

Ankhers

I am not seeing the same behaviour.

iex(1)> Mongo.find(pid, "coll", %{}) |> Enum.to_list
[
  %{"_id" => #BSON.ObjectId<5e8f5cd37f1ab13c7b1d99a9>, "coll" => 1},
  %{"_id" => #BSON.ObjectId<5e8f5cd67f1ab13c7b1c6ea3>, "coll" => 2},
  %{"_id" => #BSON.ObjectId<5e8f5cd87f1ab13c7b8e3b4a>, "coll" => 5},
  %{"_id" => #BSON.ObjectId<5e8f5cd97f1ab13c7b686cc8>, "coll" => 3},
  %{"_id" => #BSON.ObjectId<5e8f5cdc7f1ab13c7bdb8c4c>, "coll" => 7},
  %{"_id" => #BSON.ObjectId<5e8f5ce17f1ab13c7b6ebc61>, "coll" => 8},
  %{"_id" => #BSON.ObjectId<5e8f5ce37f1ab13c7bae3a15>, "coll" => 10},
  %{"_id" => #BSON.ObjectId<5e8f5ce57f1ab13c7b2069ea>, "coll" => 9},
  %{"_id" => #BSON.ObjectId<5e8f5cea7f1ab13c7b1f336f>, "coll" => 4},
  %{"_id" => #BSON.ObjectId<5e8f5d0c7f1ab13c7b5b4023>, "coll" => 6}
]
iex(2)> limit = 5
5
iex(3)> page = 1
1
iex(4)> skip = (page - 1) * limit
0
iex(5)> Mongo.find(pid, "coll", %{}, limit: limit, skip: skip, sort: [coll: 1]) |> Enum.to_list
[
  %{"_id" => #BSON.ObjectId<5e8f5cd37f1ab13c7b1d99a9>, "coll" => 1},
  %{"_id" => #BSON.ObjectId<5e8f5cd67f1ab13c7b1c6ea3>, "coll" => 2},
  %{"_id" => #BSON.ObjectId<5e8f5cd97f1ab13c7b686cc8>, "coll" => 3},
  %{"_id" => #BSON.ObjectId<5e8f5cea7f1ab13c7b1f336f>, "coll" => 4},
  %{"_id" => #BSON.ObjectId<5e8f5cd87f1ab13c7b8e3b4a>, "coll" => 5}
]
iex(6)> page = 2
2
iex(7)> skip = (page - 1) * limit # Make sure you are setting the proper value for skip each time.
5
iex(8)> Mongo.find(pid, "coll", %{}, limit: limit, skip: skip, sort: [coll: 1]) |> Enum.to_list
[
  %{"_id" => #BSON.ObjectId<5e8f5d0c7f1ab13c7b5b4023>, "coll" => 6},
  %{"_id" => #BSON.ObjectId<5e8f5cdc7f1ab13c7bdb8c4c>, "coll" => 7},
  %{"_id" => #BSON.ObjectId<5e8f5ce17f1ab13c7b6ebc61>, "coll" => 8},
  %{"_id" => #BSON.ObjectId<5e8f5ce57f1ab13c7b2069ea>, "coll" => 9},
  %{"_id" => #BSON.ObjectId<5e8f5ce37f1ab13c7bae3a15>, "coll" => 10}
]

Would you be able to show something similar to what I just did there? If you can, I may be able to help figure out what is going wrong.

tduccuong

tduccuong OP

hmm I noticed that, for every returned batch, if I do Enum.reverse the list, then the order is correct as I wanted.

Sorry I don’t have an example right now and cannot share data, but here is two snippets with the later does what I want:

   order = cond do
      sort_type == Const.sort_inc -> 1
      sort_type == Const.sort_dec -> -1
      true                        -> -1
    end
    sort = cond do
      sort_by == Const.rating_sort -> [rating:     order]
      sort_by == Const.price_sort  -> [price_out:  order]
      sort_by == Const.time_sort   -> [updated_at: order]
      true                         -> [rating:     order]
    end
   opts  = [ 
      limit: batch_size,
      skip:  (batch - 1) * batch_size,
      sort:  sort ]
    ItemM |> DbSrv.query(q, opts)

and

   order = cond do
      sort_type == Const.sort_inc -> 1
      sort_type == Const.sort_dec -> -1
      true                        -> -1
    end
    sort = cond do
      sort_by == Const.rating_sort -> [rating:     order]
      sort_by == Const.price_sort  -> [price_out:  order]
      sort_by == Const.time_sort   -> [updated_at: order]
      true                         -> [rating:     order]
    end
   opts  = [ 
      limit: batch_size,
      skip:  (batch - 1) * batch_size,
      sort:  sort ]
    ItemM |> DbSrv.query(q, opts)
               |> Enum.reverse

The DbSrv module is just a convenient wrapper around Mongo module of yours.

tduccuong

tduccuong OP

My bad, I did an Enum.reduce in the DbSrv module! Sorry for ur time! Everything is fine now.

Thanks a lot!

Where Next? Top

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 15136 120
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 11030 135
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
woylie
Phoenix components for pagination, sortable tables and filter forms with Flop and (optionally) Ecto. pagination cursor pagination sorta...
New
kip
Please say hi to a new lib, Astro that aims to deliver easy-to-consume astronomy calculations of practical use. For now it only calculat...
New

Other Trending Topics Top

akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

We're in Beta

About us Mission Statement