---
title: "MongoDB索引文件破坏后导致查询错误的问题"
author: "BigBird2012"
date: "2014-09-04T10:39:17Z"
tags: []
categories: ["数据库"]
slug: "mongodb"
draft: false
lastmod: "2014-09-04T10:40:58Z"
id: 2113172
url: "https://bigbird2012.iteye.com/blog/2113172"
---

问题描述：

MongoDB在非正常情况下关闭时，可能会导致索引文件破坏，造成数据在更新时没有反映到索引上。

解决方案：

 

使用脚本，重建MongoDB所有表的索引。

```
var names  = db.getCollectionNames();
for( var i in names ){
    var name = names[i];
    print(name);
     
    var coll = db.getCollection(name);
    coll.reIndex();
}
```

 

 原创地址：[http://genuinecx.blog.51cto.com/2890523/1530240](http://genuinecx.blog.51cto.com/2890523/1530240)