2

I want to add an index to my mongo collection outside of the interactive Mongo shell.

We're building in Python, so I could set it through MongoEngine, however, I'd greatly prefer to set the indexes directly.

Can I use db.eval() to make this happen?

2 Answers 2

2

You can use collection_name.create_index API from Python.

http://api.mongodb.org/python/1.11/api/pymongo/collection.html

If using Javascript, the problem with eval() is that it needs a write lock, and creating an index itself needs a write lock. I do not think you can use eval() for anything that needs a write lock due to this reason.

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

1 Comment

Thanks Lobster. You can create an index in the background, which I believe removes the need for a lock. db.eval() failed for me, so I'm game for your solution
0

You can use QuerySet.ensure_index.

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.