building an index is a problem that can cause long-time write locks, MongoDB when indexing at the foreground requires a write lock (and does not give up temporarily), if the collection has a large amount of data, it usually takes a long time to build the index, which is particularly prone to problems.
the solution is simple, MongoDB provides two types of indexed access, one of which is background no longer takes a long time to occupy a write lock, and another non- background Way,
It takes a long time to occupy the lock. Use the background method to solve the problem.
For example, for a super-large table posts build an index,
Never use
Db.posts.ensureIndex ({user_id:1})
Instead, you should use
Db.posts.ensureIndex ({user_id:1},{background:1})
This article is from the DBA Sky blog, so be sure to keep this source http://9425473.blog.51cto.com/9415473/1661179
Issues caused by MongoDB build index