pymongo中的多字段索引 [英] Multiple field Indexing in 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]
这篇关于pymongo中的多字段索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!