CNode

Mongodb中随机的查询文档记录

Yym1623发布于13 年前最后回复12 年前6 回复14562 浏览0 收藏

简述,摘要:在实际应用场景中,几乎都会有随机获取数据记录的需求。而这个需求在Mongodb却不是很好实现,就目前而言,大致上有三种解决方案:

  1. 先计算出一个从0到记录总数之间的随机数,然后采用skip(yourRandomNumber)方法。
  2. 为每一条记录增设random字段,插入数据时赋值为Math.random(),查询时采用$gte和$lte。
  3. 借助Mongodb对地理空间索引(geospatial indexes)的支持,从而可以在第二种方法的基础上来实现随机记录的获取。

因为Mongodb是不建议使用skip方法的,所以这里就略去第一种方法吧。 ##方法二##

> db.twitter.save({ username: 'heroic', random: Math.random(), content: 'balabala0...' })
> db.twitter.save({ username: 'heroic', random: Math.random(), content: 'balabala1...' })
> db.twitter.save({ username: 'heroic', random: Math.random(), content: 'balabala2...' })
> db.twitter.save({ username: 'heroic', random: Math.random(), content: 'balabala3...' })
> db.twitter.save({ username: 'heroic', random: Math.random(), content: 'balabala4...' })
/* more records... */

/* create index */
> db.twitter.ensureIndex({ username: 1, random: 1 })

> rand = Math.random()
> result = db.twitter.findOne({ username: 'heroic', random: { $gte: rand } })
> if (result == null) {
>   result = db.twitter.findOne({ username: 'heroic', random: { $lte: rand } })
> }

##方法三## > db.twitter.save({ username: 'heroic', random: [Math.random(), 0], content: 'balabala0...' }) > db.twitter.save({ username: 'heroic', random: [Math.random(), 0], content: 'balabala1...' }) > db.twitter.save({ username: 'heroic', random: [Math.random(), 0], content: 'balabala2...' }) > db.twitter.save({ username: 'heroic', random: [Math.random(), 0], content: 'balabala3...' }) > db.twitter.save({ username: 'heroic', random: [Math.random(), 0], content: 'balabala4...' }) /* more records... */

/* create index */
> db.twitter.ensureIndex({ username: 1, random: '2d' })

> result = db.twitter.findOne({ username: 'heroic', random: { $near: [Math.random(), 0] } })

更多关于Mongodb地理空间索引资料,请参见这里。 目前这几种方案似乎都不是很理想,但是也没有其他办法了,所以广大程序员们就相约到Mongodb的官方jira提了相应的需求,但是目前仍然没有任何的响应。可以参见这里,围观一下。 不知道大家谁还有更好的方法吗?

查看回复

回复 (6)

H
heroicyang#1·13 年前

LZ,目前貌似就这些方法了。
另,利用地理空间索引方式其实会有一些问题的,比如当需要sort时这个方案就挂了。
Mongodb的特性注定了官方是不会提供随机获取记录的方法的。。。

I
inosqlorg#2·13 年前
Y
ym1623#4·13 年前
引用 inosqlorg感觉场景不多。 http://nosql db.com/topic/513ff72cfd6181f36811cd93

以后会用的着的,防范未然嘛,哈哈..

J
jackhutu#6·12 年前

用sort肯定是不行,和第一种方法一样,数据量大了会有问题。现在mongodb有没有更好的查询随机若干条数据的方法?

参与回复
登录后即可参与回复。登录