开发者

Why is MongoDB data saved when I remove all files from /data/db?

I just started using MonogDB with Rails 3. After doing some benchmarks, I found out that it performs 5-10 times faster than Postgresql/pg.

I decided to check how the data is stored, so I removed all the files from /data/db (that folder开发者_开发技巧 contained files like "mydb.0", "mydb.1" etc.

However I still can access data from my Rails app! Even after reloading the server.

So is there any kind of temp or cache folder with all the .json files with data?


  1. You can't delete 'data files' without stopping MongoDB.
  2. MongoDB uses caching, but in case when MongoDB was stopped cached date also emptied.
  3. If you stopped MongoDB and removed the data folder, the data is no longer accessible.
  4. MongoDB saves all data as bson, not as json.


The correct answer is that this is a UNIX feature, not mongo. (I presume you are using Linux)

Mongo uses memory mapped files as a store. Among other things this means that most probably mongo keeps all the files open all the time.

On Unix, the files are removed as soon as the last process using the files closes them, not when you issue the "rm" command.

In this case, the rm command unlinks the file (Removes the entry from the directories, so cannot be accessed again).

So, the answer is that the file will be removed by the OS as soon as you stop mongo.

Note that the file is still using disk space, even if "df" or "du" says is not. This can be confusing as you can have "disk full" errors with lots of free space reported on "df".

You can delete this files but:

  • You will not really free up space, but all Unix commands will think the oposite.
  • Mongo will not be affected (At first sight, don't really know the internals), but all data will be lost on close. Which can be usefull sometimes. (I saw this once used to setup demo servers)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜