mongodb 系列(四)MongoDB常用命令

mongodb 系列(一) mongodb 数据备份与还原

mongodb 系列(二)使用mongoTemplate的Aggregation类进行分组,分页操作

mongodb 系列(三)Windows下MongoDB安装及创建用户名和密码

mongodb 系列(四)MongoDB常用命令

mongodb 系列(五)MongoDB 学习


#  匹配corpCode 是yutong ,根据org字段分组
db.t_wintalent_position_info.aggregate( [
                        { $match : { corpCode : "yutong" } },
                        { $group: { _id: '$org'} },
] );

# 新增一个content字段,字符串类型
db.t_position_info.update({}, {$set: {content:""}}, {multi: 1});

# 删除一个字段
db.t_position_info.update({},{$unset:{'content':''}},false, true);


数据操作:
db.users.find() ,user是表名,同db.getCollection('user').find;
建议使用后者,有的地方使用前面的方式不能执行


#查看集合
show collections;

创建集合、插入:
#创建集合
create collection;    
#如果数据库中不存在集合,就创建并插入这些数据
db.student.insert({"name":"张三","age":"22","sex":"男","class":"计算机2班"});
#里面的key-value不用保持一致
db.student.insert({"name":"李四","age":"22","sex":"女","phone":"18513081650","class":"计算机1班"});
#同时插入多条数据
db.student.insert([{"name":"王五","age":"22","sex":"男","class":"计算机2班"},{"name":"赵六","age":"22","sex":"女","phone":"18513081650","class":"计算机1班"}]);

查询:
#查询全部
db.users.find() 
select * from users  

#查询指定记录
db.users.find({"age" : 27})     
select * from users where age = 27  

#and查询
db.users.find({"username" : "joe", "age" : 27}) 
select * from users where username = "joe" and age = 27  

#or查询
db.student.find({$or:[{"name":"张无忌"},{"name":"李四"}]});    
select * from users where username = "张无忌" or username = "李四"

#查询指定字段
显示ID:
db.users.find({}, {"username" : 1, "email" : 1}) 
select id, username, email from users 
不显示ID: 
db.users.find({}, {"username" : 1, "_id" : 0}) 
select username from users 
有条件查询
db.users.find({"name":"张无忌"}, {"username" : 1, "email" : 1}) 
select id, username, email from users  where name = "张无忌"

#大于、小于查询
db.users.find({"age" : {"$gte" : 18, "$lte" : 30}}) 
select * from users where age >=18 and age <= 30 
//  $lt(<) $lte(<=) $gt(>) $gte(>=)  

#不等于查询
db.users.find({"username" : {"$ne" : "joe"}}) 
select * from users where username <> "joe"  

#in查询
db.users.find({"ticket_no" : {"$in" : [725, 542, 390]}}) 
select * from users where ticket_no in (725, 542, 390)  

#not in查询
db.users.find({"ticket_no" : {"$nin" : [725, 542, 390]}}) 
select * from users where ticket_no not in (725, 542, 390)  

#获取行数
db.users.count({"recruitNum":5});
db.users.find({"recruitNum":5}).count();
select count(*) from users where recruitNum = 5

#按照sort里面key的值排序,1为正序,-1为倒序
db.user.find().sort({"age":-1});
select * from user order by age asc

# 为空查询
//查询"username"为null的记录,不包括没有此字段及为空字符串
db.users.find({"username" : {"$in" : [null], "$exists" : true}}) 
select * from users where username is null
//查询"username"为null或没有此字段的记录
db.users.find({"username" : null})
//查询"username"不为null的记录
db.users.find({"username" : {"$nin" : [null], "$exists" : true}}) 
select * from users where username is not null

# 正则查询,value是符合PCRE的表达式  
db.users.find({"name" : /joey?/i}) 

# 查询前两条
db.getCollection('t_position_info').find().limit(2)
# 跳过第一条查询前两条
db.getCollection('t_position_info').find().skip(1).limit(2)


数组查询:
// 对数组的查询, 字段fruit中,既包含"apple",又包含"banana"的纪录  
db.food.find({fruit : {$all : ["apple", "banana"]}}) 

// 对数组的查询, 字段fruit中,第3个(从0开始)元素是peach的纪录      
db.food.find({"fruit.2" : "peach"}) 

// 对数组的查询, 查询数组元素个数是3的记录,$size前面无法和其他的操作符复合使用  
db.food.find({"fruit" : {"$size" : 3}}) 

// 对数组的查询,只返回数组comments中的前十条,还可以{"$slice" : -10}, {"$slice" : [23, 10]}; 分别返回最后10条,和中间10条  
db.users.findOne(criteria, {"comments" : {"$slice" : 10}}) 

 // 嵌套查询  
db.people.find({"name.first" : "Joe", "name.last" : "Schmoe"}) 
// 嵌套查询,仅当嵌套的元素是数组时使用
db.blog.find({"comments" : {"$elemMatch" : {"author" : "joe", "score" : {"$gte" : 5}}}}) 
// 复杂的查询,$where当然是非常方便的,但效率低下。对于复杂查询,考虑的顺序应当是 正则 -> MapReduce -> $where  
db.foo.find({"$where" : "this.x + this.y == 10"}) 
 // $where可以支持javascript函数作为查询条件  
db.foo.find({"$where" : "function() { return this.x + this.y == 10; }"})
// 返回第(10, 11]条,按"x"进行排序; 三个limit的顺序是任意的,应该尽量避免skip中使用large-number  
db.foo.find().sort({"x" : 1}).limit(1).skip(10);


复杂混用:
or与regex混用,查询orgCode为0/*(包含0)或者1/*(包含1)的数据
db.getCollection('t_position_search').find
({ "companyId" : "5d314242ffdddc30e471bc18" , "$or" : [ { "orgCode" : { "$regex" : "0($|/.*)"}},{ "orgCode" : { "$regex" : "1($|/.*)"}}]}]})


elemMatch与regex混用: 查询"workPlaceCodeList" 字段数组中,包含0/4/100/106/*(包扩0/4/100/106)的数据
db.getCollection('t_position_search').find
({"companyId" : "5d314242ffdddc30e471bc18" , 
 "$or" : [ { "orgCode" : { "$regex" : "0($|/.*)"}}] , "workPlaceCodeList" : { "$elemMatch" : { "$regex" : "0/4/100/106($|/.*)"}}})
 


删除:
db.student.remove({});#删除所有数据
db.student.remove({"22":"女"});#按照条件删除所有数据
db.student.remove({"name":"张无忌"},2);#删除几条


更新:
#前:查询条件,后:修改内容  只修改一条
db.student.update({"name":"张三"},{$set:{"name":"张无忌"}});
# 修改所有记录
db.student.update({"name":"王五"},{$set:{"name":"张无忌"}},{multi:true});

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值