mongodbコマンド概要


mongod –help      
mongod  --dbpath path --auth  //    
mongo –u username –p password       

    
use admin
db.addUser('admin','pwd') //    
db.system.users.find()  //      
db.auth('admin','pwd')   //    
db.removeUser('mongodb')   //    
show users			//      
show dbs		//       
show collections	//     collection
db.printCollectionStats()  //   collection   
db.printReplicationInfo()   //        
db.repairDatabase()	//     
db.setProfilingLevel(1)  //    profiling,0=off 1=slow 2=all
show profile	//  profiling
db.copyDatabase('mail_addr','mail_addr_tmp')  //     
db.mail_addr.drop()   //  collection
db.dropDatabase()    //        

   
db.foo.save({'name':'ysz','address':{'city':'beijing','post':100096},'phone':[138,139]})  //       
db.user_addr.save({'Uid':'[email protected]','Al':['[email protected]','[email protected]']})   //      

db.foo.update({'yy':5},{'$set':{'xx':2}},upsert=true,multi=true)
  //  query    ,        ,        
db.foo.remove({'yy':5}) //  yy=5   
db.foo.remove() //       


  
db.things.ensureIndex({firstname: 1, lastname: 1}, {unique: true});
//    :1(ascending),-1(descending)
db.user_addr.ensureIndex({'Al.Em': 1})//     
db.deliver_status.getIndexes()  //      
db.deliver_status.getIndexKeys()
db.user_addr.dropIndex('Al.Em_1')  //         
db.foo.dropIndexes()	


  
db.foo.find()  //    
db.foo.findOne() //      
db.foo.find().skip(10)//   10   
db.foo.find({'msg':'Hello 1'}).limit(10)//       10   
db.deliver_status.find({'From':'[email protected]'}).sort({'Dt',-1})
// sort  
db.deliver_status.find().sort({'Ct':-1}).limit(1)
db.user_addr.count()//count  
db.foo.distinct('msg')// distinct  
db.foo.find({"timestamp": {"$gte" : 2}})//>  
db.foo.find({'address.city':'beijing'})//      
       
$all,$exists,$mod,$ne,$in,$nin,$nor,$or,$and,$size,$type
db.things.find( { x : 3, y : "foo" } );
db.things.find({j: {$ne: 3}, k: {$gt: 10} });
db.collection.find({ "field" : { $lte: value } } ); 
db.collection.find({ "field" : { $gte: value } } );
db.collection.find({ "field" : { $lt: value } } );
db.collection.find({ "field" : { $gt: value1, $lt: value2 } } ); 
db.things.find( { a: { $all: [ 2, 3 ] } } );
db.things.find( { a : { $exists : true } } );
db.things.find( { a : { $mod : [ 10 , 1 ] } } )
db.things.find({j:{$in: [2,4,6]}});
db.things.find({j:{$nin: [2,4,6]}});
db.foo.find( { $or : [ { a : 1 } , { b : 2 } ] } )
db.foo.find( { $and: [ { a: 1 }, { a: { $gt: 5 } } ] }
db.things.find( { a : { $size: 1 } } );
db.things.find( { a : { $type : 2 } } ); // matches if a is a string
db.things.find( { a : { $type : 16 } } ); // matches if a is an int
db.customers.find( { name : { $regex : 'acme.*corp', $options: 'i' } } );


  
db.deliver_status.dataSize()//  collection     
db.deliver_status.stats() //  colleciont  
db.deliver_status.totalIndexSize()//         


MongoDB     
mongoexport --help  
mongoexport -d foo -c t1 -o /data/t1.json   
mongoimport --help   
mongoimport -d foo -c t1 /data/t1.json