Mongodb计数与Findone [英] Mongodb count vs findone

查看:59
本文介绍了Mongodb计数与Findone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:有一个用户集合.我正在尝试查找具有_id=xxx的用户是否具有somevalue > 5.

My problem is: There is a collection of users. Im trying to find, does user with _id=xxx has somevalue > 5.

我想知道,使用find(...).count() > 0findOne(...) != null会更快吗?或者,也许还有其他更快捷/更好的方法?

I'm wondering, what will be faster, using find(...).count() > 0 or findOne(...) != null? Or maybe there is some other, faster/better way?

推荐答案

查询时间之间的差异几乎可以忽略不计,因为它们都限制了唯一_id的范围,因此将立即发现它们.这里唯一的优势在于count,因为数据库将返回一个int而不是整个文档.因此,您节省的时间纯粹是由于将数据从db传输到客户端.

The difference between the query times should be almost negligible, because they both limit the bounds of the unique _id, so they will be found immediately. The only slight edge here goes to the count because the db will return a int instead of an entire document. So the time you save is purely because of the transfer of the data from db to client.

话虽如此,如果您的目标是进行 exists 查询并且您不关心数据,请使用count

That being said, if your goal is to do an exists query and you don't care about the data, use the count

这篇关于Mongodb计数与Findone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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