Pymongo/MongoDB:创建索引还是确保索引? [英] Pymongo / MongoDB: create index or ensure index?

查看:57
本文介绍了Pymongo/MongoDB:创建索引还是确保索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不了解pymongo中create_indexensure_index之间的区别.在 MongoDB索引页面上,

I don't understand the difference between create_index and ensure_index in pymongo. On the MongoDB indexes page, it says

您可以通过调用 ensureIndex()

you can create an index by calling the ensureIndex()

但是在pymongo中,有两个不同的命令 ensure_index ,并且用于创建索引的文档包括:

However in pymongo there are two different commands create_index and ensure_index, and the documentation for create index has:

与create_index()不同,后者尝试 无条件创建索引, sure_index()利用了一些优势 在驱动程序内进行缓存 只尝试创建索引 可能不存在.当一个索引 由PyMongo创建(或确保) 被记住"了ttl秒. 重复调用sure_index() 在此期限内将是 轻巧的-他们不会尝试 实际创建索引.

Unlike create_index(), which attempts to create an index unconditionally, ensure_index() takes advantage of some caching within the driver such that it only attempts to create indexes that might not already exist. When an index is created (or ensured) by PyMongo it is "remembered" for ttl seconds. Repeated calls to ensure_index() within that time limit will be lightweight - they will not attempt to actually create the index.

我是否正确理解ensure_index将创建一个永久索引,还是我需要为此使用create_index?

Am I right in understanding that ensure_index will create a permanent index, or do I need to use create_index for this?

推荐答案

请记住,在Mongo 3.x中

Keep in mind that in Mongo 3.x ensureIndex is deprecated and should be discouraged.

从3.0.0版开始不推荐使用:db.collection.ensureIndex()现在是db.collection.createIndex()的别名.

Deprecated since version 3.0.0: db.collection.ensureIndex() is now an alias for db.collection.createIndex().

pymongo :

已弃用-确保此集合上存在索引.

DEPRECATED - Ensures that an index exists on this collection.

这意味着您应该始终使用create_index.

Which means that you should always use create_index.

这篇关于Pymongo/MongoDB:创建索引还是确保索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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