mongoDB命令

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongoDB命令相关的知识,希望对你有一定的参考价值。

//显示数据库
show dbs
//显示数据表
show collections
//删除数据库
db.dropDatabase()
//删除集合(删除表)
db.collection.drop() (可以把collection理解成表)
//添加一条
db.col.insert({‘aa‘:1,‘bb‘:2})
//批量添加
db.col.insert([{‘aa‘:1,‘bb‘:2},{‘cc‘:3,‘dd‘:4}])

 

 

//更新个别字段 如果不加$set
>db.col.update(
  { ‘title‘:‘MongoDB 教程‘,"bbb" : "wobianle"},
  { $set:{‘title‘:‘MongoDB‘} },
  false,
  false
)

//全部重新更新 如果没有则是添加
>db.col.save(
  {
    "_id":ObjectId("5a309723286ad4fc8f5b7a13"),
    "bbb":‘wobianle‘,
    ‘ccc‘:‘woyoubianle‘
  }
);

//查看
>db.col.find().pretty()

//更新ddd大于1的
db.col.update(
  {"ddd":{$gt:1}},
  {$set:{"url":5555}}
);

//找大于20的更新
db.col.update(
  { "count" : { $gt : 20 } },
  { $set : { "sex" : "OK","love":"iloveyou"} },
  false,
  true
);

//自增加一
db.col.update(
  { "count" : { $gt : 15 } } ,
  { $inc : { "count" : 1} },
  false,
  true
);

//删除键
db.ceshi100_1.update(
  {"_id":ObjectId("5a3115645e24d08b9cec3feb")},
  {$unset:{"title":true}}
);


//删除满足条件数据
db.col.remove({‘aaa‘:2222})
//删除找到的第一条数据 true代表只删除一条
db.col.remove({‘aaa‘:2222},true)
//删除这个表里面的所有数据
db.col.remove({})

//查找命令
db.col.find( {"title":"222"} )
db.col.find( {"title":{$gt:"222"}} )
db.col.find( {"title":{ $lt: 222 } })
db.col.find( {"title":{ $gte: 222 } }) //>=
db.col.find( {"title":{ $lte: 222 } }) //<=
db.col.find( {"title":{ $gt: value1 , $lt: value2 } })  
db.col.find( {"title" : { $ne: value} }) //不等于
db.col.find( {"title" : { $mod : [ 10 , 1 ] } }) //取模运算 title%10 == 1
db.col.find( {"title" : { $nin: [ 1, 2, 3 ] } }) //not in 不在其中
db.col.find( {"title" : { $in: [ 1, 2, 3 ] } }) //in 在其中
db.col.find( {"title" : { $size: true } }) //数组元素个数
db.col.find( {"title" : { $exists : true } }) //存在的找出来
db.col.find( {"title" : /^val.*val$/im }) //正则,类似like;“i”忽略大小写,“m”支持多行
db.col.find( {"title" : { $not : /^val.*val$/i } }) //不符合这个正则的找出来
db.col.find( {$or : [{a : 1}, {b : 2} ] }) //条件a=1的或者符合条件b=2的数据都会查询出来
db.col.find( {"title" : value , $or : [{ a : 1 } , { b : 2 }] }) title = value and (a == 1 or b ==2)
db.col.find( {"key.subkey" :value })

//排序
db.col.find().sort({ "score" : 1,"age" : -1 }) //1代表升序,-1代表降序

//其他 count(true)
db.col.find().limit(5) //如果是0 则不起作用
db.col.find().skip(5)
db.col.find().skip(5).limit(5)
db.col.find().count()
db.col.find().skip(5).limit(5).count()

db.col.find().skip(1).limit(5)
22:40 2017/12/1322:40 2017/12/13 

 

group 的语法格式
{
  $group :{
    _id : < expression > ,
    < field1 >: { < accumulator1 > : < expression1 > }
    < field1 >: { < accumulator1 > : < expression1 > }
    ... ...
  }
}

$group和$project

//语法
db.collection.aggregate([array]);

//操作符
$sort : 按照给定的字段排序结果
$limit : 限制结果数量
$skip : 忽略结果的数量
$group : 按照给定表达式组合结果
$project : 包含、排除、重命名和显示字段
$match : 查询,需要同find()一样的参数
$unwind : 分割嵌入数组到自己顶层文件

group by age
select count(*),avg(score),min(score) from biao group by name order by total
1daibiao 正序 -1 倒叙
//实例
db.ceshi500_1.aggregate(
  [
    {
      $group : {
        _id : "$name",
        count : {$sum : true},//count(*)
        total : {$sum :"$age"}, //算age这个字段所有的和
        avg : {$avg :"$score"},//算score资格字段所有的平局只
        min : {$min :"$score"},//找出score这个字段最小值
        max : {$max :"$score"} //最大值
      }
    }  
    {
      $sort : {"total":-1}
    }
  ]
)

db.ceshi500_3.aggregate(
  [
    {
      $group : {
        _id : "$name",
        count : {$sum : true},
        total : {$sum :"$age"},
        avg : {$avg :"$score"},
        min : {$min :"$score"},
        max : {$max :"$score"}
      }
    },
    {
      $sort : {"total":-1}
    }  
  ]
)

 

db.ceshi500_3.insert(
[
{‘name‘:‘aa‘,‘age‘:1,‘score‘:10},
{‘name‘:‘bb‘,‘age‘:2,‘score‘:20},
{‘name‘:‘cc‘,‘age‘:3,‘score‘:30},
{‘name‘:‘dd‘,‘age‘:4,‘score‘:40},
{‘name‘:‘ee‘,‘age‘:5,‘score‘:50},
{‘name‘:‘ff‘,‘age‘:6,‘score‘:60},
{‘name‘:‘gg‘,‘age‘:7,‘score‘:70},
{‘name‘:‘hh‘,‘age‘:8,‘score‘:80},
{‘name‘:‘ii‘,‘age‘:9,‘score‘:90},
{‘name‘:‘jj‘,‘age‘:10,‘score‘:100},
{‘name‘:‘aa‘,‘age‘:11,‘score‘:110},
{‘name‘:‘bb‘,‘age‘:12,‘score‘:120},
{‘name‘:‘cc‘,‘age‘:13,‘score‘:130},
{‘name‘:‘dd‘,‘age‘:14,‘score‘:140},
{‘name‘:‘ee‘,‘age‘:15,‘score‘:150},
{‘name‘:‘ff‘,‘age‘:16,‘score‘:160},
{‘name‘:‘gg‘,‘age‘:17,‘score‘:170},
{‘name‘:‘hh‘,‘age‘:18,‘score‘:180},
{‘name‘:‘ii‘,‘age‘:19,‘score‘:190},
{‘name‘:‘jj‘,‘age‘:20,‘score‘:200},
]);

 

 


添加普通索引
db.ceshi500_3.ensureIndex({"title":1})

查询索引
db.ceshi500_3.getIndexes()

删除索引
db.ceshi500_3.dropIndex({title:1})

添加唯一索引 注意 字段保持唯一 否则报错,删除后可以添加
//在后台运行 加索引
db.ceshi500_3.ensureIndex({"age":1},{unique:true},{background: true})

查询分析
db.ceshi500_3.find().explain()
//desc explain

?


/var/lib/mongodb
//导出数据库
mongodump -d tank -o /home/conghao/ceshimongo3/
//导入数据库
mongorestore -d ccc /home/conghao/ceshimongo3/ceshi600


//导出表
mongoexport -d ceshi600 -c ceshi600_1 -o /home/conghao/ceshimongo4/ceshi600_1.json
//导入表
mongoimport -d ceshi500 -c wodeku /home/conghao/ceshimongo4/ceshi500_1.json

 







































































































































































以上是关于mongoDB命令的主要内容,如果未能解决你的问题,请参考以下文章

Mongodb学习笔记二(Mongodb基本命令)

mongodb shell命令在哪

Mongodb学习笔记二(Mongodb基本命令)

MongoDB命令汇总

mongoDB--mongoDB的常用命令

mongodb运行mongo命令以后直接结束