簡體   English   中英

Rails + MongoDB:如何找出當前記錄的位置?

[英]Rails + MongoDB: How to find out how what's the position of current record?

這是一個從MongoDB中獲取記錄的查詢:

Article.where({'_id' => { "$gt" => params[:id]}}).sort({:_id => 1}).first

假設集合中有10條記錄,這是最新的8條記錄。 如何從集合中的10條記錄中找到該記錄在位置8上?

您不能在Mongo中執行以下操作:ID是隨機生成的哈希,並且不能保證像關系數據庫ID一樣是順序的。

相反,我將按created_at排序,因為只要字段存在,該IS就保證是連續的。 考慮這樣的事情:

article = Article.find(params[:id])
next = Article.where({"created_at" => {"$gt" => article.created_at}}).desc(:created_at).first

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM