Mongodb数据库命令端常用操作

查询操作

Mongodb-SpringMvc下Query数据库操作SQL

http://blog.csdn.net/xiaohulunb/article/details/27828381

1.查询所有

> db.foo.find()
{ "_id" : ObjectId("5389aa1df06b88aaa313746a"), "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "_id" : ObjectId("5389aaa4afce65313a5614f7"), "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "_id" : ObjectId("5389aabaafce65313a5614f8"), "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
{ "_id" : ObjectId("5389aac5afce65313a5614f9"), "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }

2.显示指定列

第一个{} 放where条件 第二个{} 指定哪些列显示和不显示 (0表示不显示 >0表示显示)

后面演示使用{‘_id‘:0} 默认隐藏‘_id列’减少显示量

> db.foo.find({},{'_id':0,'name':1,'user':1})
{ "name" : "yiwa", "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "user" : { "phone" : [ 63, 137 ] } }

3.使用and操作

#名字是yiwa且年龄是25岁

> db.foo.find({'name':'yiwa','age':25},{'_id':0})
{ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }

4.使用or操作

#名字是yiwa或者年龄是75岁

> db.foo.find({'$or':[{'name':'yiwa'},{'age':75}]},{'_id':0})
{ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }

5.使用<, <=, >, >= ($lt, $lte, $gt, $gte )操作,取模运算$mod

#年龄在 15<= x <=75 岁

> db.foo.find({'age':{'$gte':15,'$lte':75}},{'_id':0})
{ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }

# 对age%3==1的取模结果

> db.foo.find({'age':{'$mod':[3,1]}},{'_id':0})
{ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }

6.使用in, not in ($in, $nin)

#名字不是siwa且年龄在[15,25,85]

> db.foo.find({'name':{'$nin':['siwa']},'age':{'$in':[15,25,85]}},{'_id':0})
{ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }

7.匹配null操作

#名字是null的

> db.foo.find({'name':null},{'_id':0})
> 

8.使用like (mongoDB 支持正则表达式)

#名字like%iwa%的

#名字like  yi%的

> db.foo.find({'name':/iwa/},{'_id':0})
{ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
> db.foo.find({'name':/^yi/},{'_id':0})
{ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }

9.使用distinct、count查询

> db.foo.distinct('name')
[ "yiwa", "erwa", "sanwa", "siwa" ]
> db.foo.count()
4

10.数组查询 (mongoDB自己特有的)(all,size)

#电话中含有186的

> db.foo.find({'user.phone':186},{'_id':0})
{ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }

#电话中含有188,186的

> db.foo.find({'user.phone':{'$all':[188,186]}},{'_id':0})
{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }

#电话中有2个值的

> db.foo.find({'user.phone':{'$size':2}},{'_id':0})
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }

11.exists判断是否存在,type判断类型,Sort排序

#name中值是字符型,age中值是整型,按name升序,age降序

> db.foo.find({'name':{'$type':2},'age':{'$type':16}},{'_id':0}).sort({'name':1,'age':-1})
{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }

#name中值存在的:true

#name中值不存在的:false

> db.foo.find({'name':{'$exists':true}},{'_id':0})
{ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
> db.foo.find({'name':{'$exists':false}},{'_id':0})
> 

12.$elemMatch数组元素匹配

#插入测试数据

> db.foo.save({x:[{'a':1,'b':5},999,'liw',{'a':12},{'b':100}]})
WriteResult({ "nInserted" : 1 })

#查询某元素中a=1,b=5的元素

> db.foo.find({'x':{'$elemMatch':{'a':1,b:{'$gt':4}}}},{'_id':0})
{ "x" : [ { "a" : 1, "b" : 5 }, 999, "liw", { "a" : 12 }, { "b" : 100 } ] }
> db.foo.find({'x.a':1,'x.b':5},{'_id':0})
{ "x" : [ { "a" : 1, "b" : 5 }, 999, "liw", { "a" : 12 }, { "b" : 100 } ] }

更新操作

1.update( criteria, objNew, upsert, multi)、save() 方法

criteria : update的查询条件,类似sql update查询内where后面的

objNew   : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的

upsert   : 这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。

multi    : mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。

save()方法相当于upsert与multi
都为true时候

> db.foo.find({},{'_id':0})
{ "name" : "yiwa", "age" : 55, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
> db.foo.update({'age':{$gte:30}},{$set:{'age':55}},fasle,false)
2014-05-31T19:36:05.407+0800 ReferenceError: fasle is not defined
> db.foo.update({'age':{$gte:30}},{$set:{'age':55}},false,false)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.foo.find({},{'_id':0})
{ "name" : "yiwa", "age" : 55, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
> db.foo.update({'age':{$gte:30}},{$set:{'age':56}},false,false)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.foo.find({},{'_id':0})
{ "name" : "yiwa", "age" : 56, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
> db.foo.update({'age':{$gte:300}},{$set:{'age':56}},true,false)
WriteResult({
	"nMatched" : 0,
	"nUpserted" : 1,
	"nModified" : 0,
	"_id" : ObjectId("5389bee8afce65313a5614fa")
})
> db.foo.find({},{'_id':0})
{ "name" : "yiwa", "age" : 56, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
{ "age" : 56 }
> db.foo.update({'age':{$gte:30}},{$set:{'age':56}},true,true)
WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 2 })
> db.foo.find({},{'_id':0})
{ "name" : "yiwa", "age" : 56, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 56, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
{ "age" : 56 }

2.$inc 对于数字字段的值增加value

#年龄大于30的 全部age值增加20

> db.foo.find({},{'_id':0})
{ "name" : "yiwa", "age" : 58, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 56, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
> db.foo.update({'age':{$gte:30}},{$inc:{'age':20}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.foo.find({},{'_id':0})
{ "name" : "yiwa", "age" : 78, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 56, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }

3.$set 相当于sql的set field = value

#年龄=56的,设置为名字=‘laoda‘,年龄=65

> db.foo.find({},{'_id':0})
{ "name" : "yiwa", "age" : 78, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "erwa", "age" : 56, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
> db.foo.update({'age':56},{$set:{'name':'laoda','age':65}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.foo.find({},{'_id':0})
{ "name" : "yiwa", "age" : 78, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "laoda", "age" : 65, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }

#只更新了一条数据,因为 multi
默认为false

4.$unset 删除字段

#查询name=‘laoda‘,user字段存在的数据中,删除age=65的age字段

> db.foo.find({},{'_id':0})
{ "name" : "yiwa", "age" : 78, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "laoda", "age" : 65, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
{ "age" : 65, "name" : "laoda" }
> db.foo.update({'name':'laoda','user':{$exists:true}},{$unset:{"age":65}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.foo.find({},{'_id':0})
{ "name" : "yiwa", "age" : 78, "user" : { "phone" : [ 123, 13, 186 ] } }
{ "name" : "laoda", "user" : { "phone" : [ 63, 188, 13, 186 ] } }
{ "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } }
{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
{ "age" : 65, "name" : "laoda" }

5.$push 数组下操作

#把value追加到field里面去,field一定要是数组类型才行,如果field不存在,会新增一个数组类型加进去

> db.array.find({},{'_id':0})
{ "age" : 65, "name" : "laoda" }
> db.array.update({'name':'laoda','age':65},{$push:{"phone":65}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{'_id':0})
{ "age" : 65, "name" : "laoda", "phone" : [ 65 ] }
> db.array.update({'name':'laoda','age':65},{$push:{"phone":[65,75,{'iphone':'188'},85]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{'_id':0})
{ "age" : 65, "name" : "laoda", "phone" : [ 65, [ 65, 75, { "iphone" : "188" }, 85 ] ] }

6.$pushAll 数组下操作

#一次可以追加多个值到数组

> db.array.find({},{'_id':0})
{ "age" : 65, "name" : "laoda" }
> db.array.update({'name':'laoda','age':65},{$pushAll:{"phone":[111,222]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{'_id':0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111, 222 ] }
> db.array.update({'name':'laoda','age':65},{$pushAll:{"phone":[111,222]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{'_id':0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222 ] }

7.$addToSet
数组操作

#增加一个值到数组内,而且只有当这个值不在数组内才增加

#插入2次发现,此值存在的时候不插入

> db.array.find({},{'_id':0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ] ] }
> db.array.find({},{'_id':0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ] ] }
> db.array.update({'name':'laoda','age':65},{$addToSet:{"phone":333}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{'_id':0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ], 333 ] }
> db.array.update({'name':'laoda','age':65},{$addToSet:{"phone":333}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.array.find({},{'_id':0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ], 333 ] }

8.$pop 删除数组内的一个值

#删除最后一个值:{
$pop : { field : 1 } }删除第一个值:{ $pop : { field : -1 } }

注意,只能删除一个值,也就是说只能用1或-1,而不能用2或-2来删除两条

> db.array.find({},{'_id':0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ], 333 ] }
> db.array.update({'name':'laoda','age':65},{$pop:{"phone":1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{'_id':0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ] ] }
> db.array.update({'name':'laoda','age':65},{$pop:{"phone":-1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{'_id':0})
{ "age" : 65, "name" : "laoda", "phone" : [ 222, 111, 222, [ 111, 222 ] ] }
> db.array.update({'name':'laoda','age':65},{$pop:{"phone":2}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{'_id':0})
{ "age" : 65, "name" : "laoda", "phone" : [ 222, 111, 222 ] }
> db.array.update({'name':'laoda','age':65},{$pop:{"phone":333}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{'_id':0})
{ "age" : 65, "name" : "laoda", "phone" : [ 222, 111 ] }
> db.array.update({'name':'laoda','age':65},{$pop:{"phone":-333}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{'_id':0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111 ] }

#测试发现,只要是正整数从最后删除,负数从头部删除。

9.$pull 数组field内删除一个等于value值

> db.array.find({},{'_id':0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111, 333 ] }
> db.array.update({'name':'laoda','age':65},{$pull:{"phone":333}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{'_id':0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111 ] }

10.$pullAll
数组field内删除多个值

> db.array.find({},{'_id':0})
{ "age" : 65, "name" : "laoda", "phone" : [ 111, 333, 222 ] }
> db.array.update({'name':'laoda','age':65},{$pullAll:{"phone":[111,222]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.array.find({},{'_id':0})
{ "age" : 65, "name" : "laoda", "phone" : [ 333 ] }

Mongodb数据库命令端常用操作,布布扣,bubuko.com

时间: 2024-10-24 11:40:22

Mongodb数据库命令端常用操作的相关文章

Mongodb数据库命令端经常使用操作

数据库基本命令操作 数据库经常使用命令 1.Help查看命令提示 help db.help(); db.yourColl.help(); db.youColl.find().help(); rs.help(); 2.切换/创建数据库 use yourDB;  当创建一个集合(table)的时候会自己主动创建当前数据库 3.查询全部数据库 show dbs; 4.删除当前使用数据库 db.dropDatabase(); 5.从指定主机上克隆数据库 db.cloneDatabase("127.0.0

MongoDB 副本集的常用操作及原理

本文是对MongoDB副本集常用操作的一个汇总,同时也穿插着介绍了操作背后的原理及注意点. 结合之前的文章:MongoDB副本集的搭建,大家可以在较短的时间内熟悉MongoDB的搭建和管理. 下面的操作主要分为两个部分: 1. 修改节点状态 主要包括: 1> 将Primary节点降级为Secondary节点 2> 冻结Secondary节点 3> 强制Secondary节点进入维护模式 2. 修改副本集的配置 1> 添加节点 2> 删除节点 3> 将Secondary节

[MongoDB] - 数据的增删改操作

在前一篇中简单的介绍了一些基本操作命令,现在分别针对这些命令进行比较详细的说明: 一.数据插入 插入数据使用命令insert,insert的参数只有一个,就是要插入的文档BSON数据.MongoDB的shell客户端没有提供批量插入的api,如果需要批量插入,那么需要使用javascript脚本来插入数据. db.users.insert({name:"tom",pwd:"123",sex:"m"}) #### 单独的插入数据for(var i=

MongoDB使用小结 一些常用操作分享【转载】

MongoDB的使用之前也分享过一篇,稍微高阶点:见这里:<MongoDB使用小结> 1.shell登陆和显示 假设在本机上有一个端口为17380的MongoDB服务,假设已经把mongo bin文件加入到系统PATH下. 登陆:mongo --port 17380 显示DB:show dbs 进入某DB:use test_cswuyg 显示集合:show tables 2.简单查找查找文档:db.test_mac_id.find({'a': 'b'}) 删除文档:db.test_mac_id

MongoDB的安装及常用操作

MongoDB MongoDB简介 MongoDB是一款跨平台面向文档的数据库.可以实现高性能.高可用性,并且能够轻松扩展.是一个基于分布式文件存储的开源数据系统,在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB也是也是一个介于关系数据库和非关系型数据库之间的产品,是非关系型数据库中功能最丰富.最像关系型数据库的.MongoDB不在有"行"的概念,其运行方式主要两个概念:集合(collection)与文档(document). MongoDB的特点 MongoDB

MongoDB副本集的常用操作及原理

下面的操作主要分为两个部分: 修改节点状态 主要包括: 将Primary节点降级为Secondary节点冻结Secondary节点强制Secondary节点进入维护模式2.?修改副本集的配置 添加节点删除节点将Secondary节点设置为延迟备份节点将Secondary节点设置为隐藏节点替换当前的副本集成员设置副本集节点的优先级阻止Secondary节点升级为Primary节点如何设置没有投票权的Secondary节点禁用chainingAllowed为Secondary节点显式指定复制源禁止S

mongo数据库命令行常用命令

help                                     #mongodb支持的命令 show dbs                                 #查看所有数据库 use test db                                       #查看当前连接在哪个数据库 show collections                         #查看所有的collection db.help()              

MongoDB分片集群常用操作

下架主节点: db.adminCommand({replSetStepDown : 1, force : true}) 删除节点: rs.remove("IP:PORT") 新增节点: rs.add({_id: ***, host:'IP:PORT',"key":value}) 原文地址:https://www.cnblogs.com/zhangtianyuan/p/8664203.html

每篇半小时1天入门MongoDB——4.MongoDB索引介绍及数据库命令操作

准备工作 继续连接到mongo C:\Users\zouqi>mongo MongoDB shell version: 3.0.7 connecting to: test 查看数据库和集合 > show dbs demo 0.078GB local 0.078GB myDatabase 0.078GB myTest 0.078GB > use myTest switched to db myTest > show collections persons system.indexes