mongodb命令总结
  TEZNKK3IfmPf 2023年11月15日 26 0


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':'yushunzhi@sohu.com','Al':['test-1@sohu.com','test-2@sohu.com']})   //存储数组对象

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':'yushunzhi@sohu.com'}).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

 

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月15日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2023年11月14日   56   0   0 mongodb
  TEZNKK3IfmPf   2023年11月14日   19   0   0 mongodb
  TEZNKK3IfmPf   2023年11月14日   22   0   0 mongodbmysql
  TEZNKK3IfmPf   2023年11月15日   41   0   0 shiromongodb
  TEZNKK3IfmPf   2023年11月15日   29   0   0 mongodbjava
  TEZNKK3IfmPf   2024年03月29日   57   0   0 mongodb
  TEZNKK3IfmPf   2023年11月15日   26   0   0 mongodb
TEZNKK3IfmPf