如何在mongodb中查找随机记录 [英] How to find random records in mongodb

查看:321
本文介绍了如何在mongodb中查找随机记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到许多只执行1条查询的随机记录.

I want find many random records doing only 1 query.

我尝试过:

var count = db.collections.count()
var rand = function(){return Math.floor( Math.random() * count )}

db.collection.find().limit(-1).skip(rand()).next();

但这仅返回一个文档.我需要获得更多随机记录.

But this returns only one document. I need to get more random records.

我该怎么做?

推荐答案

另一种实现

db.Colletion.find().limit( 50 ).skip( _rand() * db.Collection.count() )

根据您的要求更改limit(),希望这会有所帮助....

change the limit() as per your requirement, Hope this will Help....

谢谢

这篇关于如何在mongodb中查找随机记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆