MongoDB的Shell命令

对数据进行操作时,需要注意,在非关系数据中,同一个字段,可能存了不同类型的数据,所以是否加引号,查询出来的结果可能不一样。另外,条件的key值,可以加引号,也可以直接用名字即可。

1、创建数据库:use runoob

2、简单查询(其中test1为集合表名,name为字段名):

db.Collection1.find({
    'name':'liu',
    'id':'1'
})

 3、简单插入:

db.Collection1.insert({
   'name':'gang',
   'id':'2'
})

 4、简单更新(更新该记录所有字段):

//注意这里更新,相当于先delete,再add,即更新后只剩下一个字段name。
//update(查询目标条件, 修改后的值)
db.Collection1.update({
   'name':'gang',
   'id':'2'
},
{
   'name':'gang'
})

 5、简单更新(更新该记录指定字段):

//其中$set可以看做是一个函数
db.Collection1.update(
{  
   'name':'gang'  
},  
{$set:{  
   'name':'gang1'  
}},
false,
true
)  

语句范例:db.table_name.update(where,setNew,issert,multi ); 

where:类似于sql中的update 语句where后边的查询条件 

setNew:类似于sql中update语句中set后边的部分,也就是你要更新的部分 

upsert:如果要更新的那条记录没有找到,是否插入一条新纪录,默认为false不插入,true为插入 

multi :是否更新满足条件的多条的记录,false:只更新第一条,true:更新多条,默认为false 

6、删除:

db.Collection1.remove({
   'name':'gang1'
})

7、新建索引:

//这里是联合索引,(1和-1)代表按升序或降序建立索引
db.Collection1.ensureIndex(
  {
    'id': 1, 
    'name': -1
  }, 
  {background: true}
)

8、建立唯一索引:

db.Collection1.ensureIndex(
  {'id':1}
  ,
  {unique:true}
)

 9、查询索引:

db.Collection1.getIndexes()

10、删除索引:

db.Collection1.dropIndex("index_name")

11、新建全文索引:

//对字段content建全文索引
db.Collection1.ensureIndex({content:'text'})

 12、查询全文索引:

//在content上建立全文索引
db.Collection1.find({
  $text:{
    $search:"liu"
  }
})

 

拓展:

1)查询and:条件用逗号隔开即可。

2)查询or:

db.Collection1.find({
  $or:[
  	{'id':'1'},
  	{'id':'2'}
  ]
})

 3)and和or一起使用:

db.Collection1.find({
  'id':{$gt:'1'}
  ,
  $or:[
  	{'id':'1'},
  	{'id':'2'}
  ]
})

 4)条件判断(大于、小于等):

//大于($gt)、小于($lt)、大于等于($gte)、小于等于($lte)
db.Collection1.find({
   'id':{$gt:1}
})

 5)返回记录数量:

//取记录的前2个
db.Collection1.find({
  'id':{$gt:'1'}
}).limit(2)

 6)跳过记录数:

//则先跳过1个记录,再取剩下记录的2个
db.Collection1.find({
  'id':{$gt:'1'}
}).limit(2).skip(1)

 7)排序:

//排序,1升序,-1降序
db.Collection1.find({
  'id':{$gt:'1'}
}).sort({
  "id":-1
})

 8)聚合(aggregate),主要用于处理数据(诸如统计平均值,求和等),类似sql语句中的 count(*)。

 

//$group是根据name分组(_id是分组固定值),另外和分组以前使用,还有$avg、$min等。match属于筛选,多个条件用大括号{}分开,例如排序等
//相当于select name, count(*) from Collection1 group by name
db.Collection1.aggregate([
 {$group : {
    _id : "$name",
    num_tutorial : {$sum : 1}
    first_age : {$first : "$age"}
  }
 },{
    $match: {"pages": {$gte: 5}}
 }
])
 但是,如果你不想分组,就要求记录数怎么办呢,用其他方式解决,比如:
db.Collection1.find({
    'name':'liu'
}).count()
9)MapReduce:使用 MapReduce 要实现两个函数 Map 函数和 Reduce 函数,Map 函数调用 emit(key, value), 遍历 collection 中所有的记录, 将 key 与 value 传递给 Reduce 函数进行处理。 

 
//示例:通过query条件查询符合的记录,并通过emit中的name进行分组计算出现的次数。find()打印出来数据。
db.Collection1.mapReduce( 
   function() { emit(this.name,1); }, 
   function(key, values) {return Array.sum(values)}, 
      {  
         query:{name:"liu"},  
         out:"post_total" 
      }
).find()
 10)全文检索:2.6版本以后默认开启。假如有一个文章表,content字段可以建立一个全文索引,这样就可以全文搜索文章内容了。
db.Collection1.find({
  $text:{
    $search:"liu"
  }
})
 但,正确的使用格式应该为(自己测试却报错,等待解决。。。):
db.collection.runCommand( "text",{
  search: <string>, //查询条件,例如
  filter: <document>, //过滤条件,例filter: { age: { $gt: 10 } }
  project: <document>, //要返回的字段,例project: { "name": 1 },1返回
  limit: <number>, //限制数量,例limit: 2
  language: <string> //指定语言(没有中文),例language: "spanish"
})
 另外,假如查询结果集要按相似度排序返回,可使用:
//按相似度排序,并返回socre字段
db.Collection1.find(
  {$text:{$search:"哈哈哈"}},
  {score:{$meta:"textScore"}}
).sort({score:{$meta:"textScore"}})
 条件非:
//匹配的必须有ttt,但不能包含aaa
db.Collection1.runCommand("text",{search:"ttt -aaa"})
 

偏僻知识点:

1)根据$type查询(非关系数据库中,同一个字段,存储的值可能是不同类型的):

 

//{$type:2}代表字符串String类型
db.Collection1.find({
   'name':{$type:2},
})

2)复制(即同步数据),一主一从、一主多从。

3)监控,可以查看运行状态。

4)java的使用

5)用explain()来分析查询使用索引情况。

6)不支持事务,但很多操作都是原子操作,比如:

 

{ $set : { field : value } }
{ $inc : { field : value } }
 

 

 

 

 

 

 

猜你喜欢

转载自1181731633.iteye.com/blog/2394589