MongoDB 常用命令

来源:互联网 发布:手机杜比音效软件 编辑:程序博客网 时间:2024/06/06 09:24
create DB
use newDBname;

create collection
db.newCollectionName.insert({m:"test"});

query by time
db.DOL_LIVE.find({t:{'$gte':'2011-01-01 09:32:42,485','$lte':'2012-01-01 09:32:42,485'}});

query by like "%str%"
db.DOL_LIVE.find({m:{'$regex':'.*str.*'}});

remove by time
db.DOL_LIVE.remove({t:{'$gte':'2011-01-01 09:32:42,485','$lte':'2012-01-01 09:32:42,485'}})

find most recent records(skip most records);
db.DOL_LIVE.find().count()
db.DOL_LIVE.find().skip(10000).limit(5)

Create a Capped Collection (To create a capped collection limited to 40 KB)
db.createCollection("mycoll", {capped:true, size:40*1024})

Convert a Collection to Capped
db.runCommand({"convertToCapped": "mycoll", size: 100*1024*1024});

check if a collection is Capped
db.myCollection.isCapped()

check stats of db
db.stats()

To reclaim deleted space, use either of the following:
--compact, which defragments deleted space. compact requires up to 2 gigabytes of extra disk space to run. Do not use compact if you are critically low on disk space.
--repairDatabase, which rebuilds the database. Both options require additional disk space to run. For details, see Recover MongoDB Data following Unexpected Shutdown.
[Warning]: repairDatabase requires enough free disk space to hold both the old and new database files while the repair is running. Be aware that repairDatabase will block all other operations and may take a long time to complete.