Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method createIndex() not callable on a collection

In the doc of the createIndex they say db.collection.createIndex(keys, options) so i call createIndex() with the code below. The name of my database is articles and the name of the collection is bce. Inside bce there is already a document with the field article.

class Stockage():
    def __init__(self):
        self.connexion()

    def connexion(self):
        client = pymongo.MongoClient("localhost", 27017)
        self.bce = client.articles.bce

sql = Stockage()
sql.bce.createIndex({article : "text"})

And i have the following error :

Traceback (most recent call last):

  File "<ipython-input-1132-fc8762d315d1>", line 1, in <module>
    sql.bce.createIndex({article : "text"})

  File "C:\ProgramData\Anaconda3\lib\site-packages\pymongo\collection.py", line 3321, in __call__
    self.__name.split(".")[-1])

TypeError: 'Collection' object is not callable. If you meant to call the 'createIndex' method on a 'Collection' object it is failing because no such method exists.

Is not bce a collection ?

like image 641
TmSmth Avatar asked Nov 06 '18 16:11

TmSmth


People also ask

Why is 'collection' object is not callable?

% -> 1773 self.__name.split (".") [-1]) TypeError: 'Collection' object is not callable. If you meant to call the 'insert_one' method on a 'Collection' object it is failing because no such method exists. There are a few posts online that discuss this error but all seem to be when the user calls a deprecated name.

How do I create an index on a collection?

Creates indexes on collections. db.collection.createIndex () takes the following parameters: A document that contains the field and value pairs where the field is the index key and the value describes the type of index for that field. For an ascending index on a field, specify a value of 1; for descending index, specify a value of -1.

How do I remove an index object from a collection?

To remove an Index object from a collection, use the Delete method on the collection. This example uses the CreateIndex method to create two new Index objects and then appends them to the Indexes collection of the Employees TableDef object.

How to build an index on a collection in MongoDB?

The db.collection.createIndex () method is used to builds an index on a collection. A document that contains the field and value pairs where the field is the index key and the value describes the type of index for that field. For an ascending index on a field, specify a value of 1; for the descending index, specify a value of -1.


1 Answers

This is because in Pymongo the method is called create_index() instead of createIndex() as it is named in the mongo shell.

It also has a different format for the parameter compared to its mongo shell counterpart. Instead of accepting a document/dict as index specification, it accepts a list of tuples instead. This is because you cannot guarantee the ordering of dict keys in Python.

So the correct line should be:

sql.bce.create_index([("article", pymongo.TEXT)])

More details are available in the Pymongo Collection page.

like image 168
kevinadi Avatar answered Nov 05 '22 03:11

kevinadi