pymongo中的多字段索引 [英] Multiple field Indexing in pymongo

查看:147
本文介绍了pymongo中的多字段索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过pymongo为我的mongodb集合应用索引.我正在使用

I am trying to apply indexing for my mongodb collection through pymongo. I am using

db[collection_name].ensure_index([("field_name" , "text"),("unique", 1), ("dropDups" , 1)])

,并且有效.但是现在如何将其应用于多个领域? 像这样

and it works. But now how can apply it for multiple fields? Something like this

db[collection_name].ensure_index([("field_name1" , "text"),("field_name2", "text"),("field_name3", "text"),("unique", 1), ("dropDups" , 1)])

我知道我们可以使用db.collection.ensureIndex({"$**":"text"},{"name":"TextIndex"}) 在mongo shell中,但我不想索引所有字段.有人可以帮我吗?

I know we can use db.collection.ensureIndex({"$**":"text"},{"name":"TextIndex"}) in mongo shell but I dont want to Index all the fields. Can anybody help me out?

推荐答案

>>> from pymongo import IndexModel, ASCENDING, DESCENDING
>>> index1 = IndexModel([("hello", DESCENDING),
...                      ("world", ASCENDING)], name="hello_world")

在pymongo的文档中提到了这一点

This was mentioned in the doc for pymongo

db[collection_name].create_index([("field_name1" , TEXT),("field_name2", TEXT)],name="index_name"))

这将在[field_name1,field_name2]上提供一个复合索引

This will provide a composite index on [field_name1,field_name2]

http://api .mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.create_indexes

这篇关于pymongo中的多字段索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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