本文介绍了在mongo中随机订购结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现Mongo的命令语法中没有与"ORDER BY RAND()"等效的SQL(https://jira.mongodb.org/browse/SERVER-533)

I've recently discovered that Mongo has no SQL equivalent to "ORDER BY RAND()" in it's command syntax (https://jira.mongodb.org/browse/SERVER-533)

我在 http://cookbook.mongodb.org/patterns/random-attribute/坦白说,向文档中添加随机属性感觉就像是黑客.这是行不通的,因为这对我要随机化的任何给定查询都设置了隐式限制.

I've seen the recommendation at http://cookbook.mongodb.org/patterns/random-attribute/ and frankly, adding a random attribute to a document feels like a hack. This won't work because this places an implicit limit to any given query I want to randomize.

另一个被广泛建议的建议是选择一个随机索引来抵消.由于我的文档的插入顺序,这将导致其中一个字符串字段按字母顺序排列,这对于我的网站用户而言不会感觉很随意.

The other widely given suggestion is to choose a random index to offset from. Because of the order that my documents were inserted in, that will result in one of the string fields being alphabetized, which won't feel very random to a user of my site.

对于如何通过代码解决此问题,我有一些想法,但是我感觉好像缺少了一个更加显而易见的本机解决方案.有人对如何更优雅地解决这个问题有想法或想法吗?

I have a couple ideas on how I could solve this via code, but I feel like I'm missing a more obvious and native solution. Does anyone have a thought or idea on how to solve this more elegantly?

推荐答案

我必须同意:最简单的方法是在文档中安装一个随机值.同样,也不需要很大的值范围-您选择的数字取决于查询的预期结果大小(在大多数情况下,1,000-1,000,000个不同的整数应该就足够了.)

I have to agree: the easiest thing to do is to install a random value into your documents. There need not be a tremendously large range of values, either -- the number you choose depends on the expected result size for your queries (1,000 - 1,000,000 distinct integers ought to be enough for most cases).

运行查询时,不必担心随机字段-而是对其进行索引并使用它进行排序.由于随机数与文档之间没有对应关系,因此您应该获得相当随机的结果.请注意,冲突可能会导致文档以自然顺序退回.

When you run your query, don't worry about the random field -- instead, index it and use it to sort. Since there is no correspondence between the random number and the document, you should get fairly random results. Note that collisions will likely result in documents being returned in natural order.

虽然此肯定是,但是您有一条非常简单的逃生路线:鉴于MongoDB具有无模式的性质,只要服务器中支持随机排序,就可以停止包含随机字段.如果大小是一个问题,则可以运行批处理作业以从现有文档中删除该字段.如果精心设计,客户端代码应该不会有重大变化.

While this is certainly a hack, you have a very easy escape route: given MongoDB's schema-free nature, you can simply stop including the random field once there is support for random sort in the server. If size is an issue, you could run a batch job to remove the field from existing documents. There shouldn't be a significant change in your client code if you design it carefully.

另一种选择是认真思考将为给定查询随机化并返回的结果数.仅在客户端代码中进行混洗可能不会太昂贵(即,如果您仅考虑最近的10,000个帖子).

An alternative option would be to think long and hard about the number of results that will be randomized and returned for a given query. It may not be overly expensive to simply do shuffling in client code (i.e., if you only consider the most recent 10,000 posts).

这篇关于在mongo中随机订购结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 12:24