4

in mongodb's cmd you use the command:

db.test2.createIndex({ "type" : 1, "amount" : 1, "loss" : 1, "price" : 1, "create_date" : 1 }, {unique:true})

in python i can write methods for the 'find_one' command easily ie:

@staticmethod
def find_one(collection, query):
    return Database.DATABASE[collection].find_one(query)

however writing one to create an index throws an error, ie:

@staticmethod
def create_index(collection):
    return Database.DATABASE[collection].createIndex({ "type" : 1, "amount" : 1, "loss" : 1, "price" : 1, "create_date" : 1 }, {unique:true})

gives the error:

return Database.DATABASE[collection].createIndex({ "type" : 1, "amount" : 1, "loss" : 1, "price" : 1, "create_date" : 1 }, {unique:true})
NameError: name 'unique' is not defined

1 Answer 1

3

The method for creating index in pymongo is create_index. You should do something like this -

my_collection.create_index([("type", pymongo.ASCENDING),("amount", pymongo.ASCENDING),("loss", pymongo.ASCENDING),("price", pymongo.ASCENDING),("create_date", pymongo.ASCENDING)], unique=True)

Check the documentation here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.