tduccuong

tduccuong

MongoDb cursor API how to?

Hi all,

I am new to Elixir and are using this lib https://github.com/ankhers/mongodb 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!

Most Liked

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

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.

Where Next?

Popular in Libraries Top

Qqwy
TypeCheck: Fast and flexible runtime type-checking for your Elixir projects. Core ideas Type- and function specifications are const...
336 13801 100
New
RobertDober
Earmark is a pure-Elixir Markdown converter. It is intended to be used as a library (just call Earmark.as_html), but can also be used as...
239 11851 134
New
tmbb
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps: bureaucrat - which contains a bunch ...
New
tompave
Hello there, I would like to share a feature toggles library (AKA feature flags) I’ve been working on. The main package is FunWithFlags...
New
nikokozak
Hello all, I’ve been working on Svonix - a library for quickly integrating Svelte components into Phoenix views. It’s a much-needed succ...
New
kelvinst
Hey everyone! Well, we made this lib a while ago and now we decided to finally go out and public with it! It’s a tool for creating and m...
New
danschultzer
In short Plug n’ play OAuth 2.0 provider library. Just set up a resource owner schema with Ecto (your user schema), install the dependen...
New
aditya7iyengar
Rummage.Ecto and Rummage.Phoenix provide ways to perform Searching, Sorting and Pagination over Ecto queries and Phoenix collections. Fo...
New
Jskalc
Hi! Today, after a couple weeks of development I’ve released v0.1 of LiveVue. It’s a seamless integration of Vue and Phoenix LiveView, i...
New
versilov
Could not wait for the missing Elixir ML libraries to appear, so, I wrote one myself, taking https://github.com/sdwolfz/exlearn as a foun...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Sub Categories:

We're in Beta

About us Mission Statement