- 浏览: 229512 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
hl174:
写的不错,赞一个
在Java中字节与十六进制的相互转换主要思想有两点 -
l540151663:
也是个好方法
String为空判断
1:登陆进入bin目录下(win10 win+x 命令提示符管理员)
C:\Program Files\MongoDB\Server\3.2\bin
运行登陆命令
mongo 10.77.20.xx
2:退出登陆
exit
mongo命令直接加MongoDB服务器的IP地址(比如:mongo 10.77.20.xx),就可以利用Mongo的默认端口号(27017)登陆Mongo,然后便能够进行简单的命令行操作。
至于退出,直接exit,然后回车就好了。
3:查看服务器上的数据库
show dbs;
4:查看当前数据库
db ;
5:切换数据库
use test
6:创建数据库
use test2
7:删除数据库
> db.dropDatabase()
{ "dropped" : "test2", "ok" : 1 }
> show dbs
-------------------------详细教程-----------------------
MongoDB命令行操作
本文专门介绍MongoDB的命令行操作。其实,这些操作在MongoDB官网提供的Quick Reference上都有,但是英文的,为了方便,这里将其稍微整理下,方便查阅。
这里用来做测试的是远端(10.77.20.xx)的Mongo数据库。
1、登录和退出
mongo命令直接加MongoDB服务器的IP地址(比如:mongo 10.77.20.xx),就可以利用Mongo的默认端口号(27017)登陆Mongo,然后便能够进行简单的命令行操作。
至于退出,直接exit,然后回车就好了。
[plain]
$ mongo 10.77.20.xx
MongoDB shell version: 2.0.4
connecting to: 10.77.20.xx/test
> show collections
> exit
bye
从以上可以看出,登录后mongo会自动连上一个名为test的数据库。如果这个数据库不存在,那么mongo会自动建立一个名为test的数据库。上面的例子,由于Mongo服务器上没有名为test的db,因此,mongo新建了一个空的名为test的db。其中,没有任何collection。
2、database级操作
[plain]
2.1 查看服务器上的数据库
> show dbs
admin (empty)
back_up (empty)
blogtest 0.203125GB
local 44.056640625GB
test (empty)
2.2 切换数据库
切换到blogtest数据库(从默认的test数据库)
> use blogtest
switched to db blogtest
mongo中,db代表当前使用的数据库。这样,db就从原来的test,变为现在的blogtest数据库。
2.3 查看当前数据库中的所有集合
> show collections
book
system.indexes
user
2.4 创建数据库
mongo中创建数据库采用的也是use命令,如果use后面跟的数据库名不存在,那么mongo将会新建该数据库。不过,实际上只执行use命令后,mongo是不会新建该数据库的,直到你像该数据库中插入了数据。
> use test2
switched to db test2
> show dbs
admin (empty)
back_up (empty)
blogtest 0.203125GB
local 44.056640625GB
test (empty)
到这里并没有看到刚才新建的test2数据库。
> db.hello.insert({"name":"testdb"})
该操作会在test2数据库中新建一个hello集合,并在其中插入一条记录。
> show dbs
admin (empty)
back_up (empty)
blogtest 0.203125GB
local 44.056640625GB
test (empty)
test2 0.203125GB
> show collections
hello
system.indexes
这样,便可以看到mongo的确创建了test2数据库,其中有一个hello集合。
2.5 删除数据库
> db.dropDatabase()
{ "dropped" : "test2", "ok" : 1 }
> show dbs
admin (empty)
back_up (empty)
blogtest 0.203125GB
local 44.056640625GB
test (empty)
2.6 查看当前数据库
> db
test2
可以看出删除test2数据库之后,当前的db还是指向它,只有当切换数据库之后,test2才会彻底消失。
3、collection级操作
[plain]
3.1 新建collection
> db.createCollection("Hello")
{ "ok" : 1 }
> show collections
Hello
system.indexes
从上面2.4也可以看出,直接向一个不存在的collection中插入数据也能创建一个collection。
> db.hello2.insert({"name":"lfqy"})
> show collections
Hello
hello2
system.indexes
3.2 删除collection
> db.Hello.drop()
true
返回true说明删除成功,false说明没有删除成功。
> db.hello.drop()
false
不存在名为hello的collection,因此,删除失败。
3.3 重命名collection
将hello2集合重命名为HELLO
> show collections
hello2
system.indexes
> db.hello2.renameCollection("HELLO")
{ "ok" : 1 }
> show collections
HELLO
system.indexes
3.4 查看当前数据库中的所有collection
>show collections
3.5 建立索引在HELLO集合上,建立对ID字段的索引,1代表升序。
>db.HELLO.ensureIndex({ID:1})
4、Record级的操作
这一小节从这里开始,我们用事先存在的blogtest数据库做测试,其中有两个Collection,一个是book,另一个是user。
4.1 插入操作
[plain]
4.1.1 向user集合中插入两条记录
> db.user.insert({'name':'Gal Gadot','gender':'female','age':28,'salary':11000})
> db.user.insert({'name':'Mikie Hara','gender':'female','age':26,'salary':7000})
4.1.2 同样也可以用save完成类似的插入操作
> db.user.save({'name':'Wentworth Earl Miller','gender':'male','age':41,'salary':33000})
4.2 查找操作
4.2.1 查找集合中的所有记录
[plain]
> db.user.find()
{ "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 15 }
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13 }
4.2.2 查找集合中的符合条件的记录
[plain]
(1)单一条件
a)Exact Equal:
查询age为了23的数据
> db.user.find({"age":23})
{ "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 15 }
b)Great Than:
查询salary大于5000的数据
> db.user.find({salary:{$gt:5000}})
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
c)Fuzzy Match
查询name中包含'a'的数据
> db.user.find({name:/a/})
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
查询name以G打头的数据
> db.user.find({name:/^G/})
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
(2)多条件"与"
查询age小于30,salary大于6000的数据
> db.user.find({age:{$lt:30},salary:{$gt:6000}})
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
(3)多条件"或"
查询age小于25,或者salary大于10000的记录
> db.user.find({$or:[{salary:{$gt:10000}},{age:{$lt:25}}]})
{ "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 15 }
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
4.2.3 查询第一条记录
将上面的find替换为findOne()可以查找符合条件的第一条记录。
[plain]
将上面的find替换为findOne()可以查找符合条件的第一条记录。
> db.user.findOne({$or:[{salary:{$gt:10000}},{age:{$lt:25}}]})
{
"_id" : ObjectId("52442736d8947fb501000001"),
"name" : "lfqy",
"gender" : "male",
"age" : 23,
"salary" : 15
}
4.2.4 查询记录的指定字段
[plain]
查询user集合中所有记录的name,age,salary,sex_orientation字段
> db.user.find({},{name:1,age:1,salary:1,sex_orientation:true})
{ "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "age" : 23, "salary" : 15 }
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
注意:这里的1表示显示此列的意思,也可以用true表示。
4.2.5 查询指定字段的数据,并去重。
[plain]
查询gender字段的数据,并去掉重复数据
> db.user.distinct('gender')
[ "male", "female" ]
4.2.6 对查询结果集的操作
[plain]
(1)Pretty Print
为了方便,mongo也提供了pretty print工具,db.collection.pretty()或者是db.collection.forEach(printjson)
> db.user.find().pretty()
{
"_id" : ObjectId("52442736d8947fb501000001"),
"name" : "lfqy",
"gender" : "male",
"age" : 23,
"salary" : 15
}
{
"_id" : ObjectId("52453cfb25e437dfea8fd4f4"),
"name" : "Gal Gadot",
"gender" : "female",
"age" : 28,
"salary" : 11000
}
{
"_id" : ObjectId("52453d8525e437dfea8fd4f5"),
"name" : "Mikie Hara",
"gender" : "female",
"age" : 26,
"salary" : 7000
}
{
"_id" : ObjectId("52453e2125e437dfea8fd4f6"),
"name" : "Wentworth Earl Miller",
"gender" : "male",
"age" : 41,
"salary" : 33000
}
{
"_id" : ObjectId("52454155d8947fb70d000000"),
"name" : "not known",
"sex_orientation" : "male",
"age" : 13
}
(2)指定结果集显示的条目
a)显示结果集中的前3条记录
> db.user.find().limit(3)
{ "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 15 }
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
b)查询第1条以后的所有数据
> db.user.find().skip(1)
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
c)对结果集排序
升序
> db.user.find().sort({salary:1})
{ "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 15 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
降序
> db.user.find().sort({salary:-1})
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 15 }
4.2.7 统计查询结果中记录的条数
[plain]
(1)统计集合中的所有记录条数
> db.user.find().count()
5
(2)查询符合条件的记录数
查询salary小于4000或大于10000的记录数
> db.user.find({$or: [{salary: {$lt:4000}}, {salary: {$gt:10000}}]}).count()
4
4.3 删除操作
4.3.1 删除整个集合中的所有数据
[plain]
> db.test.insert({name:"asdf"})
> show collections
book
system.indexes
test
user
到这里新建了一个集合,名为test。
删除test中的所有记录。
> db.test.remove()
PRIMARY> show collections
book
system.indexes
test
user
> db.test.find()
可见test中的记录全部被删除。
注意db.collection.remove()和drop()的区别,remove()只是删除了集合中所有的记录,而集合中原有的索引等信息还在,而drop()则把集合相关信息整个删除(包括索引)。
4.3.2 删除集合中符合条件的所有记录
[plain]
> db.user.remove({name:'lfqy'})
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("52455cc825e437dfea8fd4f8"), "name" : "2", "gender" : "female", "age" : 28, "salary" : 2 }
{ "_id" : ObjectId("52455d8a25e437dfea8fd4fa"), "name" : "1", "gender" : "female", "age" : 28, "salary" : 1 }
> db.user.remove( {salary :{$lt:10}})
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
4.3.3 删除集合中符合条件的一条记录
[plain]
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("52455de325e437dfea8fd4fb"), "name" : "1", "gender" : "female", "age" : 28, "salary" : 1 }
{ "_id" : ObjectId("52455de925e437dfea8fd4fc"), "name" : "2", "gender" : "female", "age" : 28, "salary" : 2 }
> db.user.remove({salary :{$lt:10}},1)
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("52455de925e437dfea8fd4fc"), "name" : "2", "gender" : "female", "age" : 28, "salary" : 2 }
当然,也可以是db.user.remove({salary :{$lt:10}},true)
4.4 更新操作
4.4.1 赋值更新
db.collection.update(criteria, objNew, upsert, multi )
criteria:update的查询条件,类似sql update查询内where后面的
objNew:update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的。
upsert : 如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
multi : mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
[plain]
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "name" : "lfqy", "gender" : "male", "age" : 28, "salary" : 1 }
{ "_id" : ObjectId("5245607525e437dfea8fd4fe"), "name" : "lfqy", "gender" : "male", "age" : 28, "salary" : 2 }
> db.user.update({name:'lfqy'},{$set:{age:23}},false,true)
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 1 }
{ "_id" : ObjectId("5245607525e437dfea8fd4fe"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 2 }
db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 1 }
{ "_id" : ObjectId("5245607525e437dfea8fd4fe"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 2 }
> db.user.update({name:'lfqy1'},{$set:{age:23}},true,true)
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 1 }
{ "_id" : ObjectId("5245607525e437dfea8fd4fe"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 2 }
{ "_id" : ObjectId("5245610881c83a5bf26fc285"), "age" : 23, "name" : "lfqy1" }
> db.user.update({name:'lfqy'},{$set:{interest:"NBA"}},false,true)
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("5245610881c83a5bf26fc285"), "age" : 23, "name" : "lfqy1" }
{ "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 1 }
{ "_id" : ObjectId("5245607525e437dfea8fd4fe"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 2 }
4.4.2 增值更新
[plain]
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("5245610881c83a5bf26fc285"), "age" : 23, "name" : "lfqy1" }
{ "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 1 }
{ "_id" : ObjectId("5245607525e437dfea8fd4fe"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 2 }
> db.user.update({gender:'female'},{$inc:{salary:50}},false,true)
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11050 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7050 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("5245610881c83a5bf26fc285"), "age" : 23, "name" : "lfqy1" }
{ "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 1 }
{ "_id" : ObjectId("5245607525e437dfea8fd4fe"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 2 }
关于更新操作(db.collection.update(criteria, objNew, upsert, multi )),要说明的是,如果upsert为true,那么在没有找到符合更新条件的情况下,mongo会在集合中插入一条记录其值满足更新条件的记录(其中的字段只有更新条件中涉及的字段,字段的值满足更新条件),然后将其更新(注意,如果更新条件是$lt这种不等式条件,那么upsert插入的记录只会包含更新操作涉及的字段,而不会有更新条件中的字段。这也很好理解,因为没法为这种字段定值,mongo索性就不取这些字段)。如果符合条件的记录中没有要更新的字段,那么mongo会为其创建该字段,并更新。
上面大致介绍了MongoDB命令行中所涉及的操作,只是为了记录和查阅。细心的也许会发现,这篇文章,越往后我的耐心越少。期待有时间能分享一些not very navie的东西。
C:\Program Files\MongoDB\Server\3.2\bin
运行登陆命令
mongo 10.77.20.xx
2:退出登陆
exit
mongo命令直接加MongoDB服务器的IP地址(比如:mongo 10.77.20.xx),就可以利用Mongo的默认端口号(27017)登陆Mongo,然后便能够进行简单的命令行操作。
至于退出,直接exit,然后回车就好了。
3:查看服务器上的数据库
show dbs;
4:查看当前数据库
db ;
5:切换数据库
use test
6:创建数据库
use test2
7:删除数据库
> db.dropDatabase()
{ "dropped" : "test2", "ok" : 1 }
> show dbs
-------------------------详细教程-----------------------
MongoDB命令行操作
本文专门介绍MongoDB的命令行操作。其实,这些操作在MongoDB官网提供的Quick Reference上都有,但是英文的,为了方便,这里将其稍微整理下,方便查阅。
这里用来做测试的是远端(10.77.20.xx)的Mongo数据库。
1、登录和退出
mongo命令直接加MongoDB服务器的IP地址(比如:mongo 10.77.20.xx),就可以利用Mongo的默认端口号(27017)登陆Mongo,然后便能够进行简单的命令行操作。
至于退出,直接exit,然后回车就好了。
[plain]
$ mongo 10.77.20.xx
MongoDB shell version: 2.0.4
connecting to: 10.77.20.xx/test
> show collections
> exit
bye
从以上可以看出,登录后mongo会自动连上一个名为test的数据库。如果这个数据库不存在,那么mongo会自动建立一个名为test的数据库。上面的例子,由于Mongo服务器上没有名为test的db,因此,mongo新建了一个空的名为test的db。其中,没有任何collection。
2、database级操作
[plain]
2.1 查看服务器上的数据库
> show dbs
admin (empty)
back_up (empty)
blogtest 0.203125GB
local 44.056640625GB
test (empty)
2.2 切换数据库
切换到blogtest数据库(从默认的test数据库)
> use blogtest
switched to db blogtest
mongo中,db代表当前使用的数据库。这样,db就从原来的test,变为现在的blogtest数据库。
2.3 查看当前数据库中的所有集合
> show collections
book
system.indexes
user
2.4 创建数据库
mongo中创建数据库采用的也是use命令,如果use后面跟的数据库名不存在,那么mongo将会新建该数据库。不过,实际上只执行use命令后,mongo是不会新建该数据库的,直到你像该数据库中插入了数据。
> use test2
switched to db test2
> show dbs
admin (empty)
back_up (empty)
blogtest 0.203125GB
local 44.056640625GB
test (empty)
到这里并没有看到刚才新建的test2数据库。
> db.hello.insert({"name":"testdb"})
该操作会在test2数据库中新建一个hello集合,并在其中插入一条记录。
> show dbs
admin (empty)
back_up (empty)
blogtest 0.203125GB
local 44.056640625GB
test (empty)
test2 0.203125GB
> show collections
hello
system.indexes
这样,便可以看到mongo的确创建了test2数据库,其中有一个hello集合。
2.5 删除数据库
> db.dropDatabase()
{ "dropped" : "test2", "ok" : 1 }
> show dbs
admin (empty)
back_up (empty)
blogtest 0.203125GB
local 44.056640625GB
test (empty)
2.6 查看当前数据库
> db
test2
可以看出删除test2数据库之后,当前的db还是指向它,只有当切换数据库之后,test2才会彻底消失。
3、collection级操作
[plain]
3.1 新建collection
> db.createCollection("Hello")
{ "ok" : 1 }
> show collections
Hello
system.indexes
从上面2.4也可以看出,直接向一个不存在的collection中插入数据也能创建一个collection。
> db.hello2.insert({"name":"lfqy"})
> show collections
Hello
hello2
system.indexes
3.2 删除collection
> db.Hello.drop()
true
返回true说明删除成功,false说明没有删除成功。
> db.hello.drop()
false
不存在名为hello的collection,因此,删除失败。
3.3 重命名collection
将hello2集合重命名为HELLO
> show collections
hello2
system.indexes
> db.hello2.renameCollection("HELLO")
{ "ok" : 1 }
> show collections
HELLO
system.indexes
3.4 查看当前数据库中的所有collection
>show collections
3.5 建立索引在HELLO集合上,建立对ID字段的索引,1代表升序。
>db.HELLO.ensureIndex({ID:1})
4、Record级的操作
这一小节从这里开始,我们用事先存在的blogtest数据库做测试,其中有两个Collection,一个是book,另一个是user。
4.1 插入操作
[plain]
4.1.1 向user集合中插入两条记录
> db.user.insert({'name':'Gal Gadot','gender':'female','age':28,'salary':11000})
> db.user.insert({'name':'Mikie Hara','gender':'female','age':26,'salary':7000})
4.1.2 同样也可以用save完成类似的插入操作
> db.user.save({'name':'Wentworth Earl Miller','gender':'male','age':41,'salary':33000})
4.2 查找操作
4.2.1 查找集合中的所有记录
[plain]
> db.user.find()
{ "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 15 }
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13 }
4.2.2 查找集合中的符合条件的记录
[plain]
(1)单一条件
a)Exact Equal:
查询age为了23的数据
> db.user.find({"age":23})
{ "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 15 }
b)Great Than:
查询salary大于5000的数据
> db.user.find({salary:{$gt:5000}})
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
c)Fuzzy Match
查询name中包含'a'的数据
> db.user.find({name:/a/})
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
查询name以G打头的数据
> db.user.find({name:/^G/})
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
(2)多条件"与"
查询age小于30,salary大于6000的数据
> db.user.find({age:{$lt:30},salary:{$gt:6000}})
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
(3)多条件"或"
查询age小于25,或者salary大于10000的记录
> db.user.find({$or:[{salary:{$gt:10000}},{age:{$lt:25}}]})
{ "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 15 }
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
4.2.3 查询第一条记录
将上面的find替换为findOne()可以查找符合条件的第一条记录。
[plain]
将上面的find替换为findOne()可以查找符合条件的第一条记录。
> db.user.findOne({$or:[{salary:{$gt:10000}},{age:{$lt:25}}]})
{
"_id" : ObjectId("52442736d8947fb501000001"),
"name" : "lfqy",
"gender" : "male",
"age" : 23,
"salary" : 15
}
4.2.4 查询记录的指定字段
[plain]
查询user集合中所有记录的name,age,salary,sex_orientation字段
> db.user.find({},{name:1,age:1,salary:1,sex_orientation:true})
{ "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "age" : 23, "salary" : 15 }
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
注意:这里的1表示显示此列的意思,也可以用true表示。
4.2.5 查询指定字段的数据,并去重。
[plain]
查询gender字段的数据,并去掉重复数据
> db.user.distinct('gender')
[ "male", "female" ]
4.2.6 对查询结果集的操作
[plain]
(1)Pretty Print
为了方便,mongo也提供了pretty print工具,db.collection.pretty()或者是db.collection.forEach(printjson)
> db.user.find().pretty()
{
"_id" : ObjectId("52442736d8947fb501000001"),
"name" : "lfqy",
"gender" : "male",
"age" : 23,
"salary" : 15
}
{
"_id" : ObjectId("52453cfb25e437dfea8fd4f4"),
"name" : "Gal Gadot",
"gender" : "female",
"age" : 28,
"salary" : 11000
}
{
"_id" : ObjectId("52453d8525e437dfea8fd4f5"),
"name" : "Mikie Hara",
"gender" : "female",
"age" : 26,
"salary" : 7000
}
{
"_id" : ObjectId("52453e2125e437dfea8fd4f6"),
"name" : "Wentworth Earl Miller",
"gender" : "male",
"age" : 41,
"salary" : 33000
}
{
"_id" : ObjectId("52454155d8947fb70d000000"),
"name" : "not known",
"sex_orientation" : "male",
"age" : 13
}
(2)指定结果集显示的条目
a)显示结果集中的前3条记录
> db.user.find().limit(3)
{ "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 15 }
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
b)查询第1条以后的所有数据
> db.user.find().skip(1)
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
c)对结果集排序
升序
> db.user.find().sort({salary:1})
{ "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 15 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
降序
> db.user.find().sort({salary:-1})
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 15 }
4.2.7 统计查询结果中记录的条数
[plain]
(1)统计集合中的所有记录条数
> db.user.find().count()
5
(2)查询符合条件的记录数
查询salary小于4000或大于10000的记录数
> db.user.find({$or: [{salary: {$lt:4000}}, {salary: {$gt:10000}}]}).count()
4
4.3 删除操作
4.3.1 删除整个集合中的所有数据
[plain]
> db.test.insert({name:"asdf"})
> show collections
book
system.indexes
test
user
到这里新建了一个集合,名为test。
删除test中的所有记录。
> db.test.remove()
PRIMARY> show collections
book
system.indexes
test
user
> db.test.find()
可见test中的记录全部被删除。
注意db.collection.remove()和drop()的区别,remove()只是删除了集合中所有的记录,而集合中原有的索引等信息还在,而drop()则把集合相关信息整个删除(包括索引)。
4.3.2 删除集合中符合条件的所有记录
[plain]
> db.user.remove({name:'lfqy'})
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("52455cc825e437dfea8fd4f8"), "name" : "2", "gender" : "female", "age" : 28, "salary" : 2 }
{ "_id" : ObjectId("52455d8a25e437dfea8fd4fa"), "name" : "1", "gender" : "female", "age" : 28, "salary" : 1 }
> db.user.remove( {salary :{$lt:10}})
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
4.3.3 删除集合中符合条件的一条记录
[plain]
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("52455de325e437dfea8fd4fb"), "name" : "1", "gender" : "female", "age" : 28, "salary" : 1 }
{ "_id" : ObjectId("52455de925e437dfea8fd4fc"), "name" : "2", "gender" : "female", "age" : 28, "salary" : 2 }
> db.user.remove({salary :{$lt:10}},1)
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("52455de925e437dfea8fd4fc"), "name" : "2", "gender" : "female", "age" : 28, "salary" : 2 }
当然,也可以是db.user.remove({salary :{$lt:10}},true)
4.4 更新操作
4.4.1 赋值更新
db.collection.update(criteria, objNew, upsert, multi )
criteria:update的查询条件,类似sql update查询内where后面的
objNew:update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的。
upsert : 如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
multi : mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
[plain]
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "name" : "lfqy", "gender" : "male", "age" : 28, "salary" : 1 }
{ "_id" : ObjectId("5245607525e437dfea8fd4fe"), "name" : "lfqy", "gender" : "male", "age" : 28, "salary" : 2 }
> db.user.update({name:'lfqy'},{$set:{age:23}},false,true)
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 1 }
{ "_id" : ObjectId("5245607525e437dfea8fd4fe"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 2 }
db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 1 }
{ "_id" : ObjectId("5245607525e437dfea8fd4fe"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 2 }
> db.user.update({name:'lfqy1'},{$set:{age:23}},true,true)
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 1 }
{ "_id" : ObjectId("5245607525e437dfea8fd4fe"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 2 }
{ "_id" : ObjectId("5245610881c83a5bf26fc285"), "age" : 23, "name" : "lfqy1" }
> db.user.update({name:'lfqy'},{$set:{interest:"NBA"}},false,true)
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("5245610881c83a5bf26fc285"), "age" : 23, "name" : "lfqy1" }
{ "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 1 }
{ "_id" : ObjectId("5245607525e437dfea8fd4fe"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 2 }
4.4.2 增值更新
[plain]
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("5245610881c83a5bf26fc285"), "age" : 23, "name" : "lfqy1" }
{ "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 1 }
{ "_id" : ObjectId("5245607525e437dfea8fd4fe"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 2 }
> db.user.update({gender:'female'},{$inc:{salary:50}},false,true)
> db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11050 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7050 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("5245610881c83a5bf26fc285"), "age" : 23, "name" : "lfqy1" }
{ "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 1 }
{ "_id" : ObjectId("5245607525e437dfea8fd4fe"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 2 }
关于更新操作(db.collection.update(criteria, objNew, upsert, multi )),要说明的是,如果upsert为true,那么在没有找到符合更新条件的情况下,mongo会在集合中插入一条记录其值满足更新条件的记录(其中的字段只有更新条件中涉及的字段,字段的值满足更新条件),然后将其更新(注意,如果更新条件是$lt这种不等式条件,那么upsert插入的记录只会包含更新操作涉及的字段,而不会有更新条件中的字段。这也很好理解,因为没法为这种字段定值,mongo索性就不取这些字段)。如果符合条件的记录中没有要更新的字段,那么mongo会为其创建该字段,并更新。
上面大致介绍了MongoDB命令行中所涉及的操作,只是为了记录和查阅。细心的也许会发现,这篇文章,越往后我的耐心越少。期待有时间能分享一些not very navie的东西。
发表评论
-
Access denied for user 'root'@'localhost' (using password:YES)
2018-11-07 14:26 595这两天在MyEclipse中开发Web项目时,连接MYSQL数 ... -
PowerDesigner导入sql脚本生成物理模型
2017-12-05 11:17 802(1)首先从sqlserver中生成脚本 (2)打开Power ... -
mysql 2006
2017-10-12 11:30 534当查询的结果集超过 max_allowed_packet 也会 ... -
修改myslq自动增长的ID数值
2017-10-10 10:30 476ALTER TABLE basic_object AUTO_ ... -
mysql 创建用户付权限及导出
2017-03-25 22:34 5971:创建用户,付权限 C:\soft\mysql\MySQL ... -
mysql 修改data的位置目录
2017-03-21 09:52 440具体操作: 一、停止MySQL 开始-cmd ne ... -
sql效率问题
2017-03-07 14:14 4841.关于SQL查询效率,100w数据,查询只要1秒,与您分享: ... -
mysql 创建用户付权限
2016-12-13 17:07 575连接数据库 mysql -u root -p 回车 ro ... -
mysql 触发器
2016-11-28 22:32 446直接在navicat.exe中设计表中的,触发器中添加 1: ... -
CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause问题
2016-11-24 11:44 439修改的最简单的办法,都把timestamp 设置为 可以为nu ... -
mysql 同行求最大最小值
2016-11-13 20:43 606SELECT GREATEST(3,5,1,8,33,99,3 ... -
redis总结一
2016-11-05 16:30 466http://edu.51cto.com/lesson/id- ... -
mysql合理配置连接池数量
2016-10-28 17:40 1171http://www.th7.cn/db/mysql/2015 ... -
mysql 存储过程例子
2016-10-28 16:11 547BEGIN DECLARE ishave int DEFAU ... -
mysql用命令备份数据库
2016-08-08 22:33 6081:导出数据库,主要用管理员身份运行cmd c:\Progra ... -
mysql 日期函数
2016-06-30 16:05 4111:查询年月日 select year(now()); sel ... -
mysql保留2位小数查询
2016-06-15 18:54 1612SELECT T1. NAME, ... -
in8 win10 安装msi 提示2502、2503的错误代码
2016-05-02 17:09 703版权声明:本文为博主原创文章,未经博主允许不得转载。 ... -
mysql总结1
2016-04-24 21:41 4791:连接数据库 mysql -u root -p 回车 ... -
oracle 执行计划详解
2016-04-10 19:21 549简介: 本文全面详细介绍oracle执行 ...
相关推荐
MongoDB 是一个高性能、开源、无模式的分布式文档数据库,被广泛用于开发现代Web和移动应用程序,特别是那些需要处理大量实时数据的应用。作为NoSQL数据库的一员,它以其灵活性、可扩展性和高可用性赢得了开发者们的...
通过阅读这份"MongoDB总结"文件,学习者将能够掌握MongoDB的基本概念、操作以及在实际项目中的应用,为进一步学习和使用MongoDB打下坚实基础。利用Markdown格式,内容易于阅读和理解,结合Typora等编辑器,学习体验...
1. **@Document** 注解:在Java实体类上使用`@Document`注解表示该类将映射到MongoDB的一个集合。例如,`Person`类通过`@Document(collection="person")`声明对应于名为"person"的集合。 2. **MongoTemplate**:`...
总结来说,MongoDB的基础操作涵盖了数据库的基本管理、数据的增删改查,而副本集和分片则是其在高可用性和可扩展性方面的核心特性。通过深入理解和熟练掌握这些知识,可以有效地利用MongoDB处理大规模的分布式数据...
MongoDB 实验报告 本实验报告旨在详细介绍 MongoDB 的安装、配置和基本操作步骤,本报告基于 CentOS 7 系统,通过一步一步的截图和文字说明,帮助读者快速掌握 MongoDB 的使用。 一、安装 MongoDB 首先,我们需要...
mongodb知识点总结, 涉及到mongodb的简单介绍 mongodb相关概念介绍 mongodb的安装(linux) mongodb的启动和连接 mongodb数据库和集合的操作,文档的增删改查相关操作 mongodb索引的建立和删除 mongodb的备份和恢复 ...
MongoDB学习总结入门篇.pdf MongoDB是一个基于分布式文件存储的数据库,旨在为WEB应用提供可扩展的高性能数据存储解决方案。下面将对MongoDB的基本概念、特点、使用原理和基本操作进行详细介绍。 1. MongoDB基本...
mongoDB总结 mongoDB是一个基于分布式文件存储的NoSQL数据库,以下是mongoDB的一些总结,包括Java API的使用、注意事项和常见问题的解决方案。 Java API使用 mongoDB提供了Java API来访问和操作数据库。使用Java ...
1. **定位配置文件**:根据提供的示例,MongoDB的配置文件位于`E:\ruanjian\MongoDB\mongod.cfg`。您可以根据自己的实际情况找到相应的配置文件。 2. **查看配置内容**: - `dbpath`: 指定MongoDB的数据文件存放...
1. **MongoDB的基本概念**:MongoDB以集合(Collections)的形式存储数据,集合相当于关系型数据库中的表。集合内包含文档(Documents),文档是JSON格式的数据结构,可以包含嵌套的键值对或数组。 2. **数据模型**...
总结来说,Spring Data MongoDB通过`MongoDBTemplate`简化了MongoDB的集成和操作,使得开发者能够专注于业务逻辑而不是底层数据库交互。对于初学者,这是一个很好的起点,可以快速上手MongoDB和Spring Data的使用。
1. MongoDB核心概念: - 文档(Document):MongoDB中的数据以键值对的形式存储,这种形式被称为文档,类似于JSON对象。 - 集合(Collection):集合是文档的集合,类似于关系数据库中的表。 - 数据库(Database...
"odbc-mongodb-1_0_14-windows" 是一个针对Windows平台的ODBC驱动程序,用于连接和操作MongoDB数据库。 在详细解释这个软件包之前,我们先来理解ODBC和MongoDB的基础知识: 1. **ODBC**:ODBC是应用程序和数据库...
### MongoDB 总结 #### 一、数据库操作 在 MongoDB 中进行数据库操作是十分重要的,以下是一些常用的操作指令: 1. **`use DATABASE_NAME`**:切换或创建数据库。当使用 `use DATABASE_NAME` 命令时,如果指定的...
1. 下载 MongoDB Windows 版 2. 设置数据文件和日志文件的存放目录 3. 启动 MongoDB 服务 4. 将 MongoDB 作为 Windows 服务随机启动 5. 安装 MongoDB VUE 在 Windows 环境下安装 MongoDB 需要下载 MongoDB Windows ...
1. **添加 MongoDB 可执行文件到 PATH 路径**: ```bash export PATH=/usr/local/mongodb7.0.2/bin:$PATH ``` 2. **创建数据存储目录和日志文件目录**: ```bash mkdir -p /usr/local/mongodb7.0.2/data/db ...
总结,MongoDB在Ubuntu 18.04 ARM 64环境下可以提供强大的文档数据库服务。通过遵循上述步骤,你可以成功安装并运行MongoDB,从而利用其优势处理非结构化数据。对于嵌入式硬件而言,MongoDB的灵活性和性能使其成为...
1. **交易支持**:MongoDB 4.0开始支持多文档ACID事务,4.4版本进一步增强了事务的稳定性和性能,这对于需要强一致性的业务场景非常重要。 2. **聚合框架优化**:MongoDB 4.4对聚合管道进行了优化,提高了查询性能...