If I have a pymongo query, in a collection with around 4000 documents, like the following:
mong = pymongo.Connection()['ASD_2']['APS2']
py_mong = mong.find_one({'plate':'123456'})
Considering there is no .explain()["cursor"]
or .explain()["nscanned"]
allowed on find_one()
, and therefore no method at hand to find out the nature of the scan, can anyone tell me if it's worth indexing the collection when I am only going to be using find_one()
?
是的,建立索引将避免线性搜索所需要的板并在对数时间内工作(快得多)。
Yes, absolutely.
How do I know?
First, it would be utterly unacceptable if it didn't. People would have complained about the surprising and unnecessary slowness of find_one
as compared to find
, and the dev team would have fixed it (or risk being viewed as dumb).
Second, I checked the code. The implementation is exactly what you'd expect, meaning find_one
is a mere wrapper around find
:
def find_one(self, spec_or_id=None, *args, **kwargs):
if spec_or_id is not None and not isinstance(spec_or_id, dict):
spec_or_id = {"_id": spec_or_id}
for result in self.find(spec_or_id, *args, **kwargs).limit(-1):
return result
return None
(pymongo version 2.4.2)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.