对 MongoDB 查询进行排序会导致返回的行数减少

发布于 2024-11-09 14:00:19 字数 1433 浏览 0 评论 0原文

我在 Mongoid(2.0.0.beta.17 和 2.0.2)/MongoDB 中排序时遇到问题。也许我对 MongoDB 的经验还不够,你们中的一些人可以帮助我理解我做错了什么?

用户级别的症状是:

  • 查询未按日期或 ID 正确排序
  • 用户帖子在看起来应该出现时却没有出现(这是类似 Twitter 的 园丁社交网站,最近的帖子有时不会显示)

irb(main):024:0> Update.all.size 

=> 551

Update.ordered.size     # (see below for definition) 

=> 490

irb(main):010:0> Update.all.select{|u| u.created_at.nil?} 

=> []

当我进入 mongo shell 并执行以下操作时:

var cursor = db.updates.find({}, {'_id': 1}).limit(600); 
while (cursor.hasNext()) printjson(cursor.next()); 

我返回 454 行。

var cursor = db.updates.find({}, {'_id': 1}).sort({created_at : 1}).limit(600); 
while (cursor.hasNext()) printjson(cursor.next());   

还返回 454 行。

db.updates.find({}).sort({created_at: -1}).limit(1); 

返回 2 月 23 日的更新。但我在 MongoDB 中有昨天的更新。 有什么想法吗?


我的模型是:

class Update 
  include Mongoid::Document 
  include Mongoid::Timestamps 
  include Paperclip 
  field :body 
  ... 
  index [[ :created_at, Mongo::DESCENDING ]] 
  ... 
  named_scope :ordered, :order_by => ([[:created_at, :desc]]) 
  ... 
end 

I'm having an issue with ordering in Mongoid (2.0.0.beta.17 and 2.0.2)/MongoDB. Perhaps I'm just not experienced enough with MongoDB, and some of you can help me understand what I'm doing wrong?

The user-level symptoms are:

  • Queries not sorting correctly by date or ID
  • User posts are not appearing when they seem like they should be (this is a Twitter-like social networking site for gardeners, and the most recent posts are sometimes not showing up)

irb(main):024:0> Update.all.size 

=> 551

Update.ordered.size     # (see below for definition) 

=> 490

irb(main):010:0> Update.all.select{|u| u.created_at.nil?} 

=> []

When I go into the mongo shell, and do:

var cursor = db.updates.find({}, {'_id': 1}).limit(600); 
while (cursor.hasNext()) printjson(cursor.next()); 

I get 454 lines returned.

var cursor = db.updates.find({}, {'_id': 1}).sort({created_at : 1}).limit(600); 
while (cursor.hasNext()) printjson(cursor.next());   

Also returns 454 lines.

db.updates.find({}).sort({created_at: -1}).limit(1); 

returns an update from February 23rd. But I have updates from yesterday in MongoDB.
Any ideas?


My model is:

class Update 
  include Mongoid::Document 
  include Mongoid::Timestamps 
  include Paperclip 
  field :body 
  ... 
  index [[ :created_at, Mongo::DESCENDING ]] 
  ... 
  named_scope :ordered, :order_by => ([[:created_at, :desc]]) 
  ... 
end 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

这个俗人 2024-11-16 14:00:19

你能打印出 created_at 字段吗?

db.updates.find({}, {created_at:1}).sort({created_at: -1}).forEach(printjson) 

您可能对保存日期字段的方式有疑问。输出是字符串吗? ISODate()?

Can you print out the created_at field?

db.updates.find({}, {created_at:1}).sort({created_at: -1}).forEach(printjson) 

You may have an issue with how you're saving your date fields. Is the output there a string? an ISODate()?

那片花海 2024-11-16 14:00:19

我不明白为什么,但执行以下操作似乎可以解决问题:

named_scope :ordered, :order_by => ([[:created_at, :desc], [:_id, :desc]])

二次排序似乎有帮助。

irb(main):022:0> update = Update.order_by([[:created_at, :desc], [:_id, :desc]])

irb(main):025:0> update.count
=> 556
irb(main):026:0> update.first.created_at
=> Sat May 28 02:53:33 -0700 2011
irb(main):027:0> update.second.created_at
=> Fri May 27 17:55:38 -0700 2011
irb(main):028:0> update.first.created_at.class
=> Time
irb(main):029:0> update.second.created_at.class
=> Time

其中第一个被排除在原始查询之外。

I didn't figure out WHY, but doing the following seems to fix the problem:

named_scope :ordered, :order_by => ([[:created_at, :desc], [:_id, :desc]])

It seems the secondary ordering helps.

irb(main):022:0> update = Update.order_by([[:created_at, :desc], [:_id, :desc]])

irb(main):025:0> update.count
=> 556
irb(main):026:0> update.first.created_at
=> Sat May 28 02:53:33 -0700 2011
irb(main):027:0> update.second.created_at
=> Fri May 27 17:55:38 -0700 2011
irb(main):028:0> update.first.created_at.class
=> Time
irb(main):029:0> update.second.created_at.class
=> Time

The first of these was being left out of the original query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文